Removing strings if found in multiple arrays

I'm having trouble deleting a string that can be found in my two arrays.

I have 2 arrays:

array1 = 1 2 3 4 5 6 7 8 9 10 array2 = 1 5 7 8 3 2 

If a string is displayed in both arrays, I want to remove it from array1 (example [7,8] ). I tried this line of code below:

 array1( find(array1(:,1) == array2(:,1)) ,:) = []; 

but I get the following error message:

Usage error == Matrix measurements must match.

What is the right way to do this?

+4
source share
2 answers

use ismember , for example, if your arrays are a and b :

 a(ismember(a,b,'rows'),:)=[]; 

must do the job.

+5
source

setdiff is more direct and allows you to save the original array if necessary:

 setdiff(array1,array2,'rows','stable') 

For reference, you can also use interect :

 [~,ia] = intersect(array1,array2,'rows'); array1(ia,:) = []; 

However, I would use setdiff .

+1
source

Source: https://habr.com/ru/post/1492183/


All Articles