I have two arrays lets say A = [1;2;4;7;10;20]; B = [1;4;8];
Now I want to find the elements of A, that are not in B i.e; [2;7;10;20]. I just need their index that is the index of the elements [2;7;10;20] in A. How can I implement this in matlab. I can use loops and all. But that is not what I want. I want an elegant solution. Suggestions?
You can do that using the ismember
function.
A = [1;2;4;7;10;20];
B = [1;4;8];
ismem = ismember(A,B);
will give you
[1 0 1 0 0 0]'
If you really need the indices, you can use find
.
find(ismem==0)
Just as a reminder, you can always use logical indexing like so:
A(~ismem)
will give you
[2 7 10 20]'
If you want the elements of A which are not in B you can use setdiff.
If you want the indices of the elements rather than their values, you can use ismember and negate the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With