How to iterate through rows of a table, such that after each iteration some computation is done and then the below row is reached?

조회 수: 27 (최근 30일)
I have this piece of code
image=("D:\ProjectI\image.tif");
for ite=1:areasize
row= r(ite,:);
xmin=xCent(1,row) - size_of_cropped_img/2;
ymin=yCent(1,row) - size_of_cropped_img/2;
crop= imcrop(image,[xmin ymin size_of_cropped_img size_of_cropped_img]);
nextrow=ite+1;
end
there are 15 rows which needs to be iterated one by one to get the cropped outputs.Until now only the first output has been extracted.
I would be greatly thankful if someone can assist.
  댓글 수: 5
Guillaume
Guillaume 2020년 1월 10일
I haven't read the whole comments and understood where the question is at now. I just want to point out that:
areasize=size(allarea);
for ite=1:areasize
%...
is bad coding. areasize is guaranteed to be a vector of at least 2 elements (more if allarea has more than 2 dimensions), so you're passing a vector as the upper bound of a for loop. Chances are you don't know how for behaves when you pass it a vector as bounds for the loop, so if the above works it's just by luck.
The above is equivalent to:
for iter = 1:size(allarea, 1) %iterate over the ROWS of allarea
Walter Roberson
Walter Roberson 2020년 2월 4일
I see that you edited your code and removed most of the statements that people were commenting on.
However in your modified code, you are still overwriting all of the variable "crop" each time through the loop.

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

답변 (1개)

Andrew Janke
Andrew Janke 2020년 1월 31일
To iterate over the rows of a table, use height() to see how high your index should go:
for iRow = 1:height(r)
% ... do work on r(iRow,:) ...
end

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by