Combining two matrices using two criteria

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.

댓글 수: 2

the cyclist
the cyclist 2016년 11월 2일
Is this homework?
What have your tried?
Mido
Mido 2016년 11월 3일
I am new to MATLAB and I have two large datasets with millions of observations. I want to combine them by transferrring some values from one matrix to another when the first two elements in each row are the same in both. After that, I will get the complete one like l matrix (The last matrix) in my example and then I can do analysis.

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

 채택된 답변

KSSV
KSSV 2016년 11월 3일

1 개 추천

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)

댓글 수: 2

Mido
Mido 2016년 11월 3일
Thanks for you help again. I really appreciate if you recommend a book or a manual that I use to learn MATLAB.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 11월 2일

댓글:

2016년 11월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by