Loop for adding matrix elements

Hello! I have a matrix, in each column I need to add several elements (rows) of different lengths to the end of the column
X=rand(577,1289) % my matrix
LimitOneStep = 0.012 % the step I need to summarize
% columlim
for i=1:10 % start of cycle
count=0 % basic counter for measuring column length (row count)
sum=0 % counter for the sum of one part of a column (add multiple rows)
LimitOne=1 % the number of lines that I need to summarize according to the step above
for j=1:544 % this is a measure of the read limit on the column (544 is the number of rows for each column)
while count<LimitOne % the very condition when my number of steps does not exceed one. it's about 83 rows per column
count=count+LimitOneStep % summarizes the step to service
countS=ceil(count/LimitOneStep ) % allows you to get an integer, because in a loop, the index cannot be a fractional number
sum=sum+X(i,countS)
end
XS(i,j)=sum;
sum=0;
end
end
Index in position 2 exceeds array bounds (must not exceed 1289).
Error in SumX(line 890)
sum=sum+X(i,countS)
writes this error, help me figure it out

7 Comments

Terminate each line by ;
Unecessarily it is printing data on the screen. It is running fine...I am not getting any error.
"in each column I need to add several elements (rows) of different lengths to the end of the column"
Can't be done...MATLAB does not support "jagged" arrays/matrices.
Only thing can do is either augment the entire matrix to the maximum length possible or convert to a cell array.
Lev Mihailov
Lev Mihailov on 28 Jul 2020
Edited: Lev Mihailov on 28 Jul 2020
I get only the first column normally, all the rest are zero in what can be problems?
just now noticed, the length of the matrix XS = 10x544, but it should be 6x1289
what does the code I want look like
for i=1:1289
count=0;
LimitOne =1;
LimitOneStep=0.012;
for j=1:544
while count<LimitOne
count=count+LimitOneStep % j(1)=0+0.012 j(84)=1.008 % the loop is triggered on j = 84
countS=ceil(count/LimitOneStep )
sum=sum+X(i,j) % 0+X(i(:,1),j(1:84)) ; 85+X(i(:,1),j(85:169))
end
XS(i,j)=sum;
sum=0; % % 0+X(i(:,2),j(1:84)) ; 85+X(i(:,2),j(85:169))
end
"what does the code I want look like"
No idea; have no klew what is is you're trying to do/expect.
Show a small example of input and the expected output and why the output is what it is and can only be what it is...got to get a clear definition of the problem before can write code to solve it.
But, as noted above, you simply cannot add an aribtrary number of elements to an array/matrix in either direction on a column or row basis.
Arrays are rectangular, not "jagged". A cell array is still regular, but it's content can vary in size by cell.
X=[2 2 2 2 ; 3 3 3 3 ; 4 4 4 4 ; 5 5 5 5 ; 2 2 2 2 ]
countS=2 % does not count the last line
SX=[5 5 5 5; 9 9 9 9] % this is the answer I want to receive
And what logic explains how one would arrive at that result from the input, pray tell?
Simply repeating nonworking code does nothing to explain the underlying rationale behind the desire.
"Can't code what can't explain"

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 28 Jul 2020

Commented:

dpb
on 29 Jul 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!