I want to execute a logical statement in a for loop.
If I have an image of size 480(height) by 640(width), I would like to scan the image in a section, this section being the whole height between width 635 to 640. I would like to find out if there are any (x,y) coordinates which are found in the cell "cellData". This cell contains a whole list of (x,y) coordinates which can be found in the whole image.
h = height
w = width
for h = 1:480
for w = 635:640
if cellData = 1;
cellData(x,y) = SecondCoordinate(x,y);
end
end
end
Basically I am trying to select a point in the section I mentioned above. The point must be from the cell "cellData". Am i doing this correctly? Will the first (x,y) coordinates that the code detects from the cellData be stored as a (x,y) coordinate in "SecondCoordinate(x,y)"?
You should have a look at find . It's not only much shorter, but also more efficient than your current approach with nested for loops.
[row, col] = find(cellData)
would return all the coordinates where cellData
is not zero.
If cellData contains other values than just ones and zeros, it would be
[row, col] = find(cellData ~= 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.