compare two columns and retrive joint values

2 vues (au cours des 30 derniers jours)
sensation
sensation le 28 Juin 2018
Commenté : sensation le 28 Juin 2018
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!

Réponse acceptée

Guillaume
Guillaume le 28 Juin 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 commentaires
Guillaume
Guillaume le 28 Juin 2018
Modifié(e) : Guillaume le 28 Juin 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
sensation
sensation le 28 Juin 2018
Perfect thanks!!!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by