Combine 2 column matrix into 1 column matrix

2 views (last 30 days)
I have a matrix A which contains x components, Note that A have multiple rows and columns.
Let's say
A=[ 1 2 3 4 0;
1.1 2.1 3.1 4.1 0;
1.2 2.2 3.2 4.2 0;
0 2.3 3.3 4.3 0;
0 2.4 3.4 4.4 0;
0 2.5 3.5 4.5 1.5;
0 2.6 3.6 4.6 1.6];
Now I want A to be
A=[ 1 2 3 4 ;
1.1 2.1 3.1 4.1 ;
1.2 2.2 3.2 4.2 ;
0 2.3 3.3 4.3 ;
0 2.4 3.4 4.4 ;
1.5 2.5 3.5 4.5 ;
1.6 2.6 3.6 4.6 ];
Note that the matrix is big so I cant use my vision to do so. I want to combine the one that has 0 as starting point (A(1,:)=0) to the one that has zero somewhere which is the first column and the condition is that the last point of first column is closer to the value of last column (you can see the last column has value of 1.5 which is closer to 1.2 that is why I combine them together)
Please helps. I am appreciated your opinions and helps.
  2 Comments
Guillaume
Guillaume on 16 Feb 2015
You haven't explained very well what you want to do.
You say that 'B is same as A' and that you 'want A to be [..] same as B'. That would mean that A stays the same.
With the example you've given, it looks like you're removing the last column and if a row of that column is not 0, replace the first column of the same row with it. Is that what you want? Probably not, as it does not involve combining any matrix.
Image Analyst
Image Analyst on 16 Feb 2015
Clear as mud. Even after reading several times and looking at the example.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
If you want to merge non-zero elements of the last column into the first column, then you can use this:
A = [1.0, 2.0, 3.0, 4.0, 0;...
1.1, 2.1, 3.1, 4.1, 0;...
1.2, 2.2, 3.2, 4.2, 0;...
0, 2.3, 3.3, 4.3, 0;...
0, 2.4, 3.4, 4.4, 0;...
0, 2.5, 3.5, 4.5, 1.5;...
0, 2.6, 3.6, 4.6, 1.6];
X = A(:,end)>0;
A(X,1) = A(X,end);
A(:,end) = [];
This gives A as
A =
1.0 2.0 3.0 4.0
1.1 2.1 3.1 4.1
1.2 2.2 3.2 4.2
0 2.3 3.3 4.3
0 2.4 3.4 4.4
1.5 2.5 3.5 4.5
1.6 2.6 3.6 4.6

More Answers (0)

Community Treasure Hunt

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

Start Hunting!