compare two columns and retrive joint values
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I have two data sets which I want to compare and retrieve corresponding data:
A1=[726834;726835;726836;726837;726838];
A2=[7;68;36;37;8];
B1=[726835;726838;727000];
B2=[5;6;8];
I want to create a joint data set that will search B1 into A1, retrieve corresponding A2 value and joint it to B2 so that resulting data set is in this example would be:
C=[68 5;8 6];
Thank you a lot!
%so far I am stucked here
[idm,idx] = ismember(A1,B1);
A1(idm) = B2(idx(idm));
Thanks!
0 Commenti
Risposta accettata
Guillaume
il 28 Giu 2018
[found, where] = ismember(B1, A1); %note the order of the inputs!
assert(all(found), 'Some values of B1 not in A1');
C = [A2(where), B2]
3 Commenti
Guillaume
il 28 Giu 2018
Modificato: Guillaume
il 28 Giu 2018
Subscript indices must either be real positive integers or logicals
I you get that error, then you are not using the code I've posted. In particular, you must have removed the line
assert(all(found), 'Some values of B1 not in A1');
which would have told you that some values in your B1 are not found in A1. This is the only reason why the next line could result in the error you've posted.
If you do not want those missing values, and the corresponding B2 to be part of the result:
[found, where] = ismember(B1, A1); %note the order of the inputs!
C = [A2(where(found)), B2(found)] %only use B1 values found in A1
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Assembly in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!