Loop to end of column

11 views (last 30 days)
owen kelly
owen kelly on 30 Jul 2017
Edited: dpb on 30 Jul 2017
I have a number of separate columns of various sizes. Im trying to create a FOR loop that reads from the start of the column to the end of the column without specifying an exact number for the loop.
  1 Comment
dpb
dpb on 30 Jul 2017
Where are these "columns"? Arrays are rectilinear and cell contents would presumably be full for each cell...

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 30 Jul 2017
Try this:
% For first column vector
for k = 1 : length(column1)
% Do stuff
end
% For second column vector
for k = 1 : length(column2)
% Do stuff
end
and so on.
  3 Comments
Image Analyst
Image Analyst on 30 Jul 2017
If P1 is a matrix, all columns must be the same height. Since you seem to be iterating over rows, and I guess 8 is the last column, but you want the 8 to be variable, do it like this:
[rows, columns] = size(P1);
for row = 2 : rows-1
P1(row + 1, columns) = P1(row, columns) + P1(row + 1, columns-1);
end
Be aware that this is a recursive operation. You're changing an element in the last column and then using that changed value -- NOT the original value -- in the next iteration. So just be aware of that because it might not be what you want.
dpb
dpb on 30 Jul 2017
Edited: dpb on 30 Jul 2017
"... matrix is P1 and I am getting data from the 8th column"
for i=2:length(column1)
P1(i+1,8)=P1(i,8)+P1(i+1,7);
Also, this will exceed the array dimension on the last iteration as it tries to access the i+1 th row and i+1 > length(P1). (Presuming, of course, that size(P1,1)>=8 or you'll operate only on the rows up to that number as length(X) is defined as max(size(X)) so the result depends on the orientation of which dimension of the array is the greater. If you always want rows, as IA does, use SIZE() with the proper argument for the direction you want.
Well, no, you're not getting data from the 8th column, you're accessing both the 7th and the 8th column.
As IA says, you're also overwriting the previously existing element of the next row which will then be utilized in the subsequent calculation going forward. I'll go one more than his comment in that I'm virtually positive this will not be the result you expect.
If you really do mean to alter that next row, then I'd venture (yes, the crystal ball is back from the shop and seems functional this morning :) ) what you'd really want for the loop would be to start at the back and iterate towards the front, thereby not clobbering the element you're getting ready to use next.
Probably the best way to proceed is to show us a small sample P1 array and then what you expect the answer to be rather than trying to describe the issue and with us only having non-functional code to look at.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!