Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
I want to merge two matricies based on a column, what is best way?

Subject: I want to merge two matricies based on a column, what is best way?

From: BobC Cadenza

Date: 10 Oct, 2010 20:06:04

Message: 1 of 2

I have two matricies, with one column in each that is unique and I will call the index. I want to merge the matricies so that I have a new matrix that contains each row from each matrix that each have the same index. How can i do this? Below is a simple example. Thanks in advance.

mat1 = [[1.2 4.2 datenum(today);5.2 2.4 datenum(today-1);2.4 1.2 datenum(today-3)];

mat2 = [[1.2 34.2 datenum(today)32.4 11.2 datenum(today-3)];

result = [[1.2 4.2 1.2 34.2 datenum(today);2.4 1.2 32.4 11.2 datenum(today-3)];

Subject: I want to merge two matricies based on a column, what is best way?

From: Roger Stafford

Date: 10 Oct, 2010 20:48:03

Message: 2 of 2

"BobC Cadenza" <bobc@mailinator.com> wrote in message <i8t6bc$7ih$1@fred.mathworks.com>...
> I have two matricies, with one column in each that is unique and I will call the index. I want to merge the matricies so that I have a new matrix that contains each row from each matrix that each have the same index. How can i do this? Below is a simple example. Thanks in advance.
>
> mat1 = [[1.2 4.2 datenum(today);5.2 2.4 datenum(today-1);2.4 1.2 datenum(today-3)];
>
> mat2 = [[1.2 34.2 datenum(today)32.4 11.2 datenum(today-3)];
>
> result = [[1.2 4.2 1.2 34.2 datenum(today);2.4 1.2 32.4 11.2 datenum(today-3)];
- - - - - - - - - -
  Your description leaves much to be desired as to its clarity. This is my best guess of what you mean:

 [c,i1,i2] = intersect(mat1(:,3),mat2(:,3));
 result = [mst1(i1,1:2),mat2(i2,1:2),c];

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us