How to iterate through rows of a table, such that after each iteration some computation is done and then the below row is reached?
Afficher commentaires plus anciens
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 commentaires
Jakob B. Nielsen
le 8 Jan 2020
First: to avoid confusion in the future, if you have an x that looks like this: x = 5,10,20 then you have one row with 3 columns in this case. The first index is the row index, and the second index is the column index. So I would, in your case, rename your "row" into "col" instead.
Next; you dont need to do a nextrow = ite+1; inside your for loop - especially because you dont use nextrow for anything :) The for loop will automatically execute a number of times specified.
I am guessing that the output you want is the crop variable. Right now, your loop will probably run "areasize" number of times (15 times, from what you write). But every time the loop runs, you will overwrite your crop variable because it is not indexed. Simply index the crop variable, and then you should end up with a 15-by-1 vector (guesswork, since I dont know your dimensions or the imcrop function), where crop(7) is your 7'th iteration etc.
for ite=1:areasize
col= r(ite,:);
xmin=xCent(1,col) - size_of_cropped_img/2;
ymin=yCent(1,col) - size_of_cropped_img/2;
crop(ite)= imcrop(image,[xmin ymin size_of_cropped_img size_of_cropped_img]);
end
Walter Roberson
le 8 Jan 2020
You are overwriting all of the variable crop during each iteration of for ite
Tony Dimichele
le 9 Jan 2020
Try out some nested for loops, you will be able to create a new file with
for i = 0 : 5
destinationFolder = 'D:\Results(i)\';
This isn't correct syntax and you will require the sprintf command to add the index to strings I'm pretty sure, but this should set you in the right direction.
Guillaume
le 10 Jan 2020
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
le 4 Fév 2020
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.
Réponses (1)
Andrew Janke
le 31 Jan 2020
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
Catégories
En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!