Ed Cota
Ed Cota

Reputation: 239

Removing certain elements of an array based on another array (MATLAB)

I have arrays

A = [7 4 6 1 2 3 5]
B = [1 5 4 0 0 2 0]

(Array A will always have length=7 with the numbers in a random order.)

I want to keep the order of A, but only retain the values if its corresponding index of B is >0.

So from the example above, I'd want to change A to [6 1 2 3]. Meaning, the 7th, 4th, and 5th elements in B equal 0, so delete values 7, 4, and 5 from A.

I'd like to do this without a loop.

Upvotes: 1

Views: 1587

Answers (1)

Jeff
Jeff

Reputation: 12418

A(B(A)>0)

or:

A(find(B(A)))

though I believe the latter is less efficient

Upvotes: 2

Related Questions