compare two columns and retrive joint values

조회 수: 1 (최근 30일)
sensation
sensation 2018년 6월 28일
댓글: sensation 2018년 6월 28일
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!

채택된 답변

Guillaume
Guillaume 2018년 6월 28일
[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
Guillaume
Guillaume 2018년 6월 28일
편집: Guillaume 2018년 6월 28일
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 2018년 6월 28일
Perfect thanks!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by