Info

This question is closed. Reopen it to edit or answer.

One function returns the correct results, the opposite not. Why?

2 views (last 30 days)
I have the following function that checks the pixels from `x` that have the membership value `near 1` and sets those pixels in `x` to `1` and return them back. The works pretty well for me:
function c = core(x, y)
tolerance = 0.01;
[ii,jj]=find (abs(y)-1 <= tolerance);
x(ii,jj)=1; % set pixels in x with y=1 to 1
idx=[ii,jj]; % indexes of pixels with y=1
c=x(abs(y)-1 <= tolerance); % values of pixels
end
Now, to the opposite function below. I just want to check the pixels that have membership values values `not equal to one` and since in my case I don't have exactly value `1` and this used `tolerance`. I wrote what I think is the opposite of the above, but my output is an `empty matrix`. Why is that? What should I change in the function below?
function s = support(x, y)
tolerance = 0.01;
[ii,jj]= find (abs(y)-1 > tolerance);
x(ii,jj)=0; % set pixels in x with y~=1 to 0
idx=[ii,jj]; % indexes of pixels with y~=1
s=x(abs(y)-1 > tolerance); % values of pixels
end
Thanks.

Answers (1)

Walter Roberson
Walter Roberson on 22 Feb 2013
Remember that find() can return vectors of values, with ii(1) corresponding to jj(i), ii(2) corresponding to jj(2) and so on. However, when ii and jj are vectors, x(ii,jj) means x accessed at all rows in ii as the intersect with all rows in jj, so ii(1) corresponding to all jj(1), jj(2), and so on, together with ii(2) matched with jj(1), jj(2) and so on.
If you have a particular reason for needing to know the row and column indices rather than just knowing where in the vector the element is, then you need to use subs2ind()
[ii,jj]= find (abs(y)-1 > tolerance);
xidx = subs2ind(size(x), ii, jj);
x(xidx) = 1;
but probably easier would be just to use the location information,
idx = find(abs(y)-1 > tolerance);
x(idx) = 1;

This question is closed.

Community Treasure Hunt

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

Start Hunting!