Combining two matrices using two criteria
3 次查看(过去 30 天)
显示 更早的评论
h = [1 4 11;
2 5 12;
4 7 13]
y = [1 4;
2 5;
3 6;
4 7]
I want for each row of matrix y to detect the first two elements in matrix h that are the same as matrix y and then copy the third element of the detected row in matrix h in a new third column in matrix y. The result should be
l = [1 4 11;
2 5 12;
3 6 0;
4 7 13]
I can't figure out the code for this.
采纳的回答
KSSV
2016-11-3
clc; clear all ;
h = [1 4 11;
2 5 12;
4 7 13] ;
y = [1 4;
2 5;
3 6;
4 7] ;
% check positions of h in y
[val,idx] = ismember(h(:,1:2),y,'rows','legacy') ;
% initilaize required matrix
iwant = zeros(size(y,1),3) ;
iwant(:,1:2) = y ;
iwant(idx,3) = h(:,3)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!