How to vectorize for..loop with nested "if" and "break" statements

2 ビュー (過去 30 日間)
Baraka Maiseli
Baraka Maiseli 2016 年 3 月 2 日
編集済み: dpb 2016 年 3 月 3 日
Dear colleagues, I am trying to vectorize the following for..loop in my matlab code:
for c=Cmin:Cmax % Cmin, Cmax are columns indices
for r=Rmin:Rmax % Rmin, Rmax are rows indices
if(img1(r, c)==1) % img1 is a binary image
x1 = r;
y1 = c;
break;
end
end
end
The problem I am facing is the inner "if" and "statement" to be included in the vectorized code. I have followed several vectorization techniques, but I haven't happened to see one that include nested conditions. Any idea please. Thank you!
  4 件のコメント
Kevin Claytor
Kevin Claytor 2016 年 3 月 2 日
Are you actually trying to determine if this is a binary image? Because there are better ways of doing that, and your code snippet above would risk misclassifying any integer-valued image as a binary image.
dpb
dpb 2016 年 3 月 3 日
編集済み: dpb 2016 年 3 月 3 日
"_it is the first occurrence , due to the break statement"_
The break only terminates the inner loop; it'll then go on and start over the outer loop. Since x1, y1 are each a single variable, you'll overwrite the first column location with the second, then the third, leaving at the end only the last column first row as the one and only pair of values retained.

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

採用された回答

Orion
Orion 2016 年 3 月 2 日
Hi, it seems you're trying to do something like
% initialize indices
Rmin = 3;
Rmax = 8;
Cmin = 2;
Cmax = 7;
% initialize a zero matrix with some values to one.
img = zeros(10);
img(4,5) = 1; % inside Rmin/Rmax, Cmin/Cmax
img(3,7) = 1; % inside Rmin/Rmax, Cmin/Cmax
img(3,8) = 1; % OUTSIDE Rmin/Rmax, Cmin/Cmax => won't be in the result
% get all the points equal to 1 in the Rmin/Rmax, Cmin/Cmax submatrix
[allx,ally] = find(img(Rmin:Rmax,Cmin:Cmax)==1);
% correct the indices to fit those in the whole matrix.
allx = allx + Rmin - 1;
ally = ally + Cmin - 1;
% get the first component :
x1 = allx(1);
y1 = ally(1);
  1 件のコメント
Baraka Maiseli
Baraka Maiseli 2016 年 3 月 3 日
Thank you Orion for a nice comment. Your code works well; I also tried a short form by Jos (next comment), which also produce a correct result.

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

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 3 月 2 日
[x1,y1] = find(img1(Rmin:Rmax,Cmin:Cmax)==1,1,'first')
and perhaps correct for the offset
x1 = x1 + Rmin - 1 ...
  2 件のコメント
Baraka Maiseli
Baraka Maiseli 2016 年 3 月 3 日
編集済み: Baraka Maiseli 2016 年 3 月 3 日
Jos, I never knew such a lengthy for..loop could be put in such a simple two-line code (I remember one of my lecturer told us in a class, "think more, write less"). Your code works very well. Now, I am trying to get another similar pair (x2,y2), which is somewhere from the end of image, img1, as shown in the illustration. Let me try to apply your idea to get the point.
Baraka Maiseli
Baraka Maiseli 2016 年 3 月 3 日
Got it! As the positions of 1's are on the borders of the bounding box, with a little offset (observed from experiments), I fixed Rmin and Cmin and computed appropriate indices:
[x1,y1] = find(img1(Rmin:Rmax,Cmin+1)==1,1,'first');
% Also, we may use [x1,y1] = find(img1(Rmin:Rmax,Cmin:Cmax)==1,1,'first');
% for searching (x1, y1) within the bounding box
x1 = x1 + Rmin; % Correction for offset
y1 = y1 + Cmin; % Correction for offset
[x2,y2] = find((img1(Rmin+1,Cmin:Cmax))==1,1,'first');
% Also, we may use [x2,y2] = find(img1(Rmin:Rmax,Cmin:Cmax)==1,1,'first');
% for searching (x2, y2) within the bounding box
x2 = x2 + Rmin; % Correction for offset
y2 = y2 + Cmin; % Correction for offset

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

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by