Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab arrays operation

Tags:

matlab

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?

like image 971
om mustafa Avatar asked Feb 27 '26 04:02

om mustafa


2 Answers

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

setdiff(b, a)
like image 85
Richie Cotton Avatar answered Mar 02 '26 14:03

Richie Cotton


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

[c, ia, ib] = intersect(a, b);
b (ib) = [];
like image 28
Itamar Katz Avatar answered Mar 02 '26 15:03

Itamar Katz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!