compare two columns and retrive joint values
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Assembly에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!