Sam
Sam

Reputation: 487

Masking in Matlab

I have encountered with a problem which might be super easy but I am spending some times on it. I will appreciate any help.

I have a matrix R (1024x1280 double) which the indexes are 0 and 1 and another matrix of F (3000x2 double), which are the location of 3000 points(of course in terms of R matrix). How could I choose the points in F matrix which that locations are 1 in the R matrix. I have tried following way but still does not give me the right answer:

siz= size(R);
fet = false(siz(1),siz(2));
fet(F(:,1),F(:,2)) = true;
[xf,yf]=find(fet==1 & R==1);`

I know that I could write a for loop in F and choose one by one point (F(1,1),F(1,2)) and check if the same point in the R matrix is equal to 1 and then save the point. But this is non efficient way. Can anybody give me a hand in this regards? Best,

Upvotes: 0

Views: 41

Answers (1)

Suever
Suever

Reputation: 65430

You can use sub2ind to convert F to linear indices and then index into R to get the booleans.

bool = logical(R(sub2ind(size(R), F(:,1), F(:,2))));
touse = F(bool,:);

Upvotes: 2

Related Questions