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

dpb
dpb 2017년 7월 30일
Where are these "columns"? Arrays are rectilinear and cell contents would presumably be full for each cell...

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2017년 7월 30일

0 개 추천

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

owen kelly
owen kelly 2017년 7월 30일
It doesn't seem to be working. Do I put this exact code down?
The name of the matrix is P1 and I am getting data from the 8th column
currently this is what I have
for i=2:length(column1)
P1(i+1,8)=P1(i,8)+P1(i+1,7);
end
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 2017년 7월 30일
편집: dpb 2017년 7월 30일
"... 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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2017년 7월 30일

편집:

dpb
2017년 7월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by