Reputation: 33
Suppose you have a vectora = [1 2 3 4 5]
and another vector b = [1 0 1 1 0]
. Is there a way in which I can get the elements in 'a' which correspond to the '1' in 'b' (i.e. ans = 1 3 4
) in MATLAB?
Upvotes: 1
Views: 46
Reputation: 713
a = 1:5;
b = logical([1 0 1 1 0])
c = a(b);
Or alternatively
a = 1:5;
b = [1 0 1 1 0]
c = a(b == 1);
Upvotes: 1