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:
merge matrices

Subject: merge matrices

From: Konst

Date: 23 Nov, 2010 11:53:03

Message: 1 of 6

I have to matrices and i want to merge them but if the values of a cell are the same for the two matrices then delete that row of the final matrix and replace it with the values of the second matrix.
Something like this
table1
     1.3203 1.3894 0.0691
    2.1636 2.4124 0.2488
    3.1728 3.6014 0.4286
table2
    1.3203 1.5829 0.2627
    2.6060 2.7719 0.1659
    3.6152 3.9055 0.2903
final table
    1.3203 1.5829 0.2627
    2.1636 2.4124 0.2488
    2.6060 2.7719 0.1659
    3.1728 3.6014 0.4286
    3.6152 3.9055 0.2903

I have found one function here that gives the final table as

    1.3203 1.3894 0.0691 1.5829 0.2627
    2.1636 2.4124 0.2488 0 0
    2.6060 0 0 2.7719 0.1659
    3.1728 3.6014 0.4286 0 0
    3.6152 0 0 3.9055 0.2903
 but i Don't need the zeros or the extra columns because they give me wrong results in some calculations I have in the end using the sum of a column..
So if anyone could help I would really appreciate it...
Thanks in advance!

Subject: merge matrices

From: Bruno Luong

Date: 23 Nov, 2010 13:18:07

Message: 2 of 6

"Konst " <konstance1@hotmail.com> wrote in message <icg9uv$p8r$1@fred.mathworks.com>...
> I have to matrices and i want to merge them but if the values of a cell are the same for the two matrices then delete that row of the final matrix and replace it with the values of the second matrix.
> Something like this
> table1
> 1.3203 1.3894 0.0691
> 2.1636 2.4124 0.2488
> 3.1728 3.6014 0.4286
> table2
> 1.3203 1.5829 0.2627
> 2.6060 2.7719 0.1659
> 3.6152 3.9055 0.2903
> final table
> 1.3203 1.5829 0.2627
> 2.1636 2.4124 0.2488
> 2.6060 2.7719 0.1659
> 3.1728 3.6014 0.4286
> 3.6152 3.9055 0.2903
>
> I have found one function here that gives the final table as
>
> 1.3203 1.3894 0.0691 1.5829 0.2627
> 2.1636 2.4124 0.2488 0 0
> 2.6060 0 0 2.7719 0.1659
> 3.1728 3.6014 0.4286 0 0
> 3.6152 0 0 3.9055 0.2903
> but i Don't need the zeros or the extra columns because they give me wrong results in some calculations I have in the end using the sum of a column..
> So if anyone could help I would really appreciate it...
> Thanks in advance!

help ISMEMBER, INTERSECT, SETDIFF

Bruno

Subject: merge matrices

From: Konst

Date: 23 Nov, 2010 13:53:04

Message: 3 of 6

 setdiff(A, B) returns the values in A that are not in B only
ismember gives a table with logical 1,0 values and not my data
intersect(A, B) returns the values common to both A and B
But I want both the common and uncommon values to the final table so I need something between setdiff and intersect..But both these commands don't merge the tables, they just return the values in the cells
Thanks very much though, it was a start!

Subject: merge matrices

From: Bruno Luong

Date: 23 Nov, 2010 14:19:04

Message: 4 of 6

"Konst " <konstance1@hotmail.com> wrote in message <icgh00$ra8$1@fred.mathworks.com>...
> setdiff(A, B) returns the values in A that are not in B only
> ismember gives a table with logical 1,0 values and not my data
> intersect(A, B) returns the values common to both A and B
> But I want both the common and uncommon values to the final table so I need something between setdiff and intersect..But both these commands don't merge the tables, they just return the values in the cells

Then you have all the tools needed to carry out the task.

Bruno

Subject: merge matrices

From: Konst

Date: 24 Nov, 2010 10:50:14

Message: 5 of 6

If I find the common and uncommon values how do I merge these two matrices without the zeros?I can't find the right way to combine the commands yet but I'll try it again..Thank you!

Subject: merge matrices

From: Bruno Luong

Date: 24 Nov, 2010 16:56:03

Message: 6 of 6

A=[1.3203 1.3894 0.0691;
2.1636 2.4124 0.2488;
3.1728 3.6014 0.4286]

B=[1.3203 1.5829 0.2627;
2.6060 2.7719 0.1659;
3.6152 3.9055 0.2903]

C = sortrows([A(~ismember(A(:,1),B(:,1)),:); B],1)

Bruno

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