How to find compare unequal size matrices and create new matrix by combining them??
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I have the following matrices:
X = [1001 1011 1001 10 130 0.8 0.2;
1001 1012 1001 11 150 0.2 0.8;
1001 1012 1001 11 131 0.5 0.5;
1001 1012 1002 11 140 0.6 0.4];
Y = [1001 1011 1001 10 112 1 122 214;
1001 1011 1001 11 221 1 134 113; % extra row
1001 1012 1001 11 98 1 58 82;
1001 1012 1001 11 68 1 59 67;
1001 1012 1002 11 32 1 56 98;
1004 1012 1005 12 67 1 89 132]; % extra row
and I want to get this :
M = [1001 1011 1001 10 130 0.8 0.2;
1001 1011 1001 11 NaN NaN NaN; % or sth equivalent to NaN
1001 1012 1001 11 150 0.2 0.8;
1001 1012 1001 11 131 0.5 0.5;
1001 1012 1002 11 140 0.6 0.4;
1004 1012 1005 12 NaN NaN NaN];
I have difficulty in finding/comparing 4 elements together of every row (M(:,1:4)), and also when the size of the matrices in not equal...
Any suggestions on which could be the right function to solve it??
Thanks,
Iro
댓글 수: 3
Sean de Wolski
2013년 5월 7일
I'm not clear on how you derived M. Can you be very specific, where does each row in M come from?
채택된 답변
Andrei Bobrov
2013년 5월 7일
ii = ismember(Y(:,1:4),X(:,1:4),'rows');
M = [Y(:,1:4),nan(size(Y,1),3)];
M(ii,5:end) = X(:,5:end);
댓글 수: 6
Andrei Bobrov
2013년 5월 10일
All right, use loc(tf) instead loc, because loc has nulls and is non logical type.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!