storing data into a vector within nested for loop

im = imread(im);
im = im2bw(im);
s = size(im);
xtot = s(2);
ytot = s(1);
for xcount = 1:xtot,
for ycount = 1:ytot,
if im(ycount,xcount) == 1
sqrt((xcount-coord(2)).^2+(ycount-coord(1)).^2)
end
end
end
end
bear with me, since i'm new to matlab.
i'm making this function to quickly calculate distances from a particular object to a known coordinate. i turned the object image into black and white, read the dimensions, then iterated along the image matrix searching for 1s (which are a part of the object). then i use the coordinates that come up as 1s and measure distance to the known coordinate. i need to store all the distances in one vector so i can take the minimum. input arguments were the image and the 2d coordinate. thanks for your help.

 採用された回答

Titus Edelhofer
Titus Edelhofer 2012 年 3 月 19 日

1 投票

Hi,
usually I would not use a loop for this kind of operations: do the following instead:
[Xcount, Ycount] = meshgrid(1:xtot, 1:ytot);
distance = sqrt((Xcount-coord(2)).^2 + (Ycount-coord(1)).^2);
Now the matrix "distance" contains all distances...
Titus

1 件のコメント

Michael
Michael 2012 年 3 月 19 日
awesome. that's exactly the function i needed to know.
thanks for your help!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by