Combine 2 column matrix into 1 column matrix

2 ビュー (過去 30 日間)
Phong Pham
Phong Pham 2015 年 2 月 16 日
編集済み: Stephen23 2015 年 2 月 16 日
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 件のコメント
Guillaume
Guillaume 2015 年 2 月 16 日
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 2015 年 2 月 16 日
Clear as mud. Even after reading several times and looking at the example.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 2 月 16 日
編集済み: Stephen23 2015 年 2 月 16 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by