Fred S
Fred S

Reputation: 1511

MATLAB: Combine matrices with different length but some identical entries in one column

Suppose I have measured two variables a and b with a different temporal resolution, e.g. I have a 7x2 matrix A (headers only for illustration):

time    value
t1      a1
t2      a2
t3      a3
t4      a4
t5      a5
t6      a6
t7      a7

and a 3x2 matrix B:

time    value
t2      b1
t4      b2
t6      b3

Is there an elegant way (i.e without looping find) to combine them in a 3x2 matrix C that only includes times where I have measured both a and b:

time    value a     value b
t2      a2          b1
t4      a4          b2
t6      a6          b3

?

Upvotes: 0

Views: 176

Answers (1)

Autonomous
Autonomous

Reputation: 9075

Perform set intersection:

[~,IA,IB]=intersect(A(:,1),B(:,1),'rows');

C=[A(IA,:) B(IB,2)];

Upvotes: 1

Related Questions