om mustafa
om mustafa

Reputation: 13

Matlab arrays operation

I have two arrays, a and b, of different size. Each one contains unique values.

I want to compare both and if any value of array a is in array b, then I want to delete it from b (e.g. a = [2 3 5], b = [1 8 6 2 3 7], results b = [1 8 6 7]).

How can it be implemented in Matlab?

Upvotes: 2

Views: 264

Answers (3)

gnovice
gnovice

Reputation: 125854

Yet another option is to use the ISMEMBER function to remove elements from b that are members of a via logical indexing:

b(ismember(b,a)) = [];

Upvotes: 3

Richie Cotton
Richie Cotton

Reputation: 121067

Use setdiff to find elements in one set but not the other.

setdiff(b, a)

Upvotes: 7

Itamar Katz
Itamar Katz

Reputation: 9645

Use intersect with 3 output arguments to get the indices of the elements to be deleted:

[c, ia, ib] = intersect(a, b);
b (ib) = [];

Upvotes: 5

Related Questions