How to match column of a one matrix with another column of second matrix?

9 ビュー (過去 30 日間)
AS
AS 2021 年 11 月 28 日
コメント済み: AS 2021 年 12 月 1 日
I have three matrices like
A =
0 0 3
0 2 3
1 2 0
1 0 0
B =
0 3 0
2 3 0
2 0 1
0 0 1
C =
3 0 0
3 0 2
0 1 2
0 1 0
I want arrange matrix B and C like matrix A means column wise they all should like similar.
Could you please help me?
  4 件のコメント
John D'Errico
John D'Errico 2021 年 11 月 28 日
So you are asking someone to show how to rearrange the columns of B and C, to match A? What if there is no exact rearrangement?
AS
AS 2021 年 11 月 28 日
yes

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

採用された回答

John D'Errico
John D'Errico 2021 年 11 月 28 日
編集済み: John D'Errico 2021 年 11 月 28 日
A = [ 0 0 3
0 2 3
1 2 0
1 0 0];
B = [ 0 3 0
2 3 0
2 0 1
0 0 1];
C = [ 3 0 0
3 0 2
0 1 2
0 1 0];
If you will accept ONLY an exact rearrangement of the columns, then we might look for a permutation matrix. Does one exist?
PAB = (normalize(A,1,'norm',2)'*normalize(B,1,'norm',2)) > 1-10*eps
PAB = 3×3 logical array
0 0 1 1 0 0 0 1 0
If the matrix PAB is a valid permutation matrix, then it will have exactly one unit element in every row and every column. Now we can transform B using the product:
B*PAB'
ans = 4×3
0 0 3 0 2 3 1 2 0 1 0 0
If you feel you really need to get the permutation vector itself, I could do this:
[~,pab] = max(PAB,[],2)
pab = 3×1
3 1 2
B(:,pab)
ans = 4×3
0 0 3 0 2 3 1 2 0 1 0 0
Similarly,
PAC = (normalize(A,1,'norm',2)'*normalize(C,1,'norm',2)) > 1-10*eps
PAC = 3×3 logical array
0 1 0 0 0 1 1 0 0
C*PAC'
ans = 4×3
0 0 3 0 2 3 1 2 0 1 0 0
Now, suppose we have a matrix that fails to have an exact rearrangeent of the columns?
D = [ 3 0 0
0 2 3
1 2 0
1 0 0];
PAD = (normalize(A,1,'norm',2)'*normalize(D,1,'norm',2)) > 1-10*eps
PAD = 3×3 logical array
0 0 0 0 1 0 0 0 0
No such permutation of the columns exists here. A simple test of that is:
sum(PAD,1)
ans = 1×3
0 1 0
sum(PAD,2)
ans = 3×1
0 1 0
Both such tests should result in vectors of purely ones if PAD were a permutation matrix.
  3 件のコメント
John D'Errico
John D'Errico 2021 年 11 月 30 日
But that does not mean there is any valid EXACT rearrangment for what you ask. I don't see your data, since you have not provided the actual data. So I cannot show you why what you are asking to do seems to have failed.
When I did ask you what was intended if an exact solution to the column rearrangement problem does not exist, all you said was "yes". Does "yes" tell me anything of significance?
AS
AS 2021 年 12 月 1 日
yes it is solved.thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by