Help using find function

15 views (last 30 days)
This is pretty basic but I'm just trying to properly get subcript indices after edge detection (logical matrix E). For some reason, linear indexing seems to be working, but whe I try to convert to subscript indices it goes all wrong. Any help would be appreciated.
indx = find(E);
mask=false(size(E));
mask(indx)=true;
imshow(mask);
[y,x] = ind2sub(size(E),indx);
mask=false(size(E));
mask(y,x)=true;
imshow(mask);

Accepted Answer

Walter Roberson
Walter Roberson on 5 Feb 2021
mask(y,x)=true;
That means the same as
for X = x
for Y = y
mask(Y, X) = true;
end
end
Which is to say that it sets all combinations of x and y.
It does not mean the same as
for index = 1 : length(y)
mask(y(index), x(index)) = true;
end
In order to do the operation you were hoping for, leave the indices as linear instead of breaking them out to row and column.
It looks to me as if the whole thing could be replaced with
mask = E ~= 0;
imshow(mask)
  1 Comment
Hannah Rosenberg
Hannah Rosenberg on 8 Feb 2021
Thank you! The for loops were very helpful for explaining what's actually going on inside of those operations.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!