Sharang Sinai Amonkar
Sharang Sinai Amonkar

Reputation: 33

Selecting elements in a vector using a binary vector

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

Answers (1)

Poelie
Poelie

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

Related Questions