comparing 2 vectors for duplicates

1 visualizzazione (ultimi 30 giorni)
Sam
Sam il 17 Mar 2016
Modificato: Adam il 17 Mar 2016
Hello,
I have a vector of 465051 x 1 elements, and a vector of 1197 x 1 elements. The 465051 vector contains all the 1197 elements, but I don't know where. So I created the following code:
[num,txt,raw] = xlsread('cg_uit_beta2'); %extracting the 465051 vector
A = txt(:,1); %extracting the 465051 vector
[num2,txt2,raw2] = xlsread('cg_uit_beta2_2'); %extracting the 1197 vector
B = txt2(:,1); %extracting the 1197 vector
L = ismember(A,B); %find rownumber of duplicates in the 465051 vector
I = find(L); %find rownumber of duplicates in the 465051 vector
So, now I know where the 1197 duplicates in the 465051 vector are. But now I just need to extract these values out of the 465051 vector... but how do I do that?
Thanks!

Risposta accettata

Adam
Adam il 17 Mar 2016
Modificato: Adam il 17 Mar 2016
[L,I] = ismember(A,B);
vals = A(I~=0);
should work. The extra 'find' step is un-necessary if you use the second output of ismember. If you prefer though:
L = ismember(A,B);
I = find(L);
vals = A(I);

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by