compare two columns and retrive joint values
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Guillaume
on 28 Jun 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 Comments
Guillaume
on 28 Jun 2018
Edited: Guillaume
on 28 Jun 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
More Answers (0)
See Also
Categories
Find more on Assembly in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!