How to find compare unequal size matrices and create new matrix by combining them??

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

Is there any way to do it (or something similar) with
find
and/or
ismember ?
Any other suggestions?
please, help!
I'm not clear on how you derived M. Can you be very specific, where does each row in M come from?
M is the result I want to get with some function, derived from X an Y. More specifically, the four first columns come from Y's first 4 columsn while the rest 3 columns come from X's last three columns, in the case where the combinations of the first 4 column of Y are the same with combinations of X's first 4 columns... e.g: if M(i,1:4) is the same to any of X(j,1:4), then assign elements of X(j,5:7) to the M(i,5:end) by discarding the existing ones..
Let me know if it makes more sense now...Thanks!! :)

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

 채택된 답변

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

Iro
Iro 2013년 5월 7일
편집: Iro 2013년 5월 7일
Thank you Andrei! It works. Do you think that this is the most efficient way to do it in terms of time and memory? (because this will have to run for some thousands of rows for some hundreds iterations until convergence...)
@Iro, this looks pretty well optimized. You could pull a few things out of the loop (like size(Y,1)) if it's the same every time.
Ok, thank you both!!
and what about when I have multiple rows with the same (r,1:4) combinations in Y? For example:
X = [1001 1011 1001 10 130 0.8 0.2;
1001 1012 1001 11 150 0.2 0.8;
1001 1012 1002 11 131 0.5 0.5;
1001 1013 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 (r,1:4) combination
1001 1011 1001 11 221 1 115 112; % extra multiple comb
1001 1012 1001 11 98 1 58 82; % multiple existing comb
1001 1012 1001 11 98 1 35 72; % multiple existing comb
1001 1012 1001 11 98 1 68 62; % multiple existing comb
1001 1012 1002 11 68 1 59 67;
1001 1013 1002 11 32 1 56 98;
1004 1012 1005 12 67 1 89 132]; % extra comb
I tried with
[tf,loc] = ismember(Y(:,1:4),X(:,1:4),'rows')
M = [Y(:,1:4),nan(size(Y,1),3)];
M(tf,5:end) = X(loc,5:end);
in order to get this:
M = [1001 1011 1001 10 130 0.8 0.2; % taken from X(1,5:end)
1001 1011 1001 11 nan nan nan;
1001 1011 1001 11 nan nan nan;
1001 1012 1001 11 150 0.2 0.8; % taken from X(2,5:end)
1001 1012 1001 11 150 0.2 0.8; % taken from X(2,5:end)
1001 1012 1001 11 150 0.2 0.8; % taken from X(2,5:end)
1001 1012 1002 11 131 0.5 0.5; % taken from X(3,5:end)
1001 1013 1002 11 140 0.6 0.4; % taken from X(4,5:end)
1004 1012 1005 12 nan nan nan]; %
but I get the error:
??? Subscript indices must either be real positive integers or logicals.
in the last line of my code.
Well I tried it with:
M(tf,5:end) = X(loc(tf),5:end);
instead, and it seems to work. However I don't really understand what is the difference between loc and loc(tf) in relation to the error above (real positive integer or logical)... any hints?
All right, use loc(tf) instead loc, because loc has nulls and is non logical type.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

질문:

Iro
2013년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by