How can I reshape a square matrix to a rectangular matrix based on its adjacency list?
2 ビュー (過去 30 日間)
古いコメントを表示
How can I reshape a square matrix to a rectangular matrix based on its adjacency list? Let's say I have the following 14x14 matrix A. If it is a graph, each node has a maximum neighbors = 6. I want to create a matrix which will be 14x6. So, each row will have maximum 6 items and the values will be the non-zero items (keeping original sequence) from the original matrix, followed by zero padding.
A =
0 -1 0 0 0 0 -2 -2 0 0 0 0 0 2
-1 0 0 0 0 0 0 0 2 0 0 0 2 2
0 0 0 -1 2 -1 0 0 0 0 0 0 0 -2
0 0 -1 0 2 0 0 0 0 0 0 0 0 0
0 0 2 2 0 0 0 0 0 0 0 0 1 2
0 0 -1 0 0 0 -1 0 0 2 0 0 0 -2
-2 0 0 0 0 -1 0 -1 0 2 0 0 0 0
-2 0 0 0 0 0 -1 0 -1 0 2 0 0 0
0 2 0 0 0 0 0 -1 0 0 2 0 -1 0
0 0 0 0 0 2 2 0 0 0 -1 2 0 0
0 0 0 0 0 0 0 2 2 -1 0 2 0 0
0 0 0 0 0 0 0 0 0 2 2 0 -1 2
0 2 0 0 1 0 0 0 -1 0 0 -1 0 0
2 2 -2 0 2 -2 0 0 0 0 0 2 0 0
Now it's adjacency list will be (I got it from Mathematica):
{{2, 7, 8, 14}},
{{1, 9, 13, 14}},
{{4, 5, 6, 14}},
{{3, 5}},
{{3, 4, 13, 14}},
{{3, 7, 10, 14}},
{{1, 6, 8, 10}},
{{1, 7, 9, 11}},
{{2, 8, 11, 13}},
{{6, 7, 11, 12}},
{{8, 9, 10, 12}},
{{10, 11, 13, 14}},
{{2, 5, 9, 12}},
{{1, 2, 3, 5, 6, 12}}
Now I need the output matrix will be:
A_reshaped =
-1 -2 -2 2 0 0
-1 2 2 2 0 0
-1 2 -1 -2 0 0
-1 2 0 0 0 0
2 2 1 2 0 0
-1 -1 2 -2 0 0
-2 -1 -1 2 0 0
-2 -1 -1 2 0 0
2 -1 2 -1 0 0
2 2 -1 2 0 0
2 2 -1 2 0 0
2 2 -1 2 0 0
2 1 -1 -1 0 0
2 2 -2 2 -2 2
0 件のコメント
採用された回答
Matt J
2022 年 3 月 23 日
編集済み: Matt J
2022 年 3 月 23 日
A=[ 0 -1 0 0 0 0 -2 -2 0 0 0 0 0 2
-1 0 0 0 0 0 0 0 2 0 0 0 2 2
0 0 0 -1 2 -1 0 0 0 0 0 0 0 -2
0 0 -1 0 2 0 0 0 0 0 0 0 0 0
0 0 2 2 0 0 0 0 0 0 0 0 1 2
0 0 -1 0 0 0 -1 0 0 2 0 0 0 -2
-2 0 0 0 0 -1 0 -1 0 2 0 0 0 0
-2 0 0 0 0 0 -1 0 -1 0 2 0 0 0
0 2 0 0 0 0 0 -1 0 0 2 0 -1 0
0 0 0 0 0 2 2 0 0 0 -1 2 0 0
0 0 0 0 0 0 0 2 2 -1 0 2 0 0
0 0 0 0 0 0 0 0 0 2 2 0 -1 2
0 2 0 0 1 0 0 0 -1 0 0 -1 0 0
2 2 -2 0 2 -2 0 0 0 0 0 2 0 0];
At=A.';
I0=(At~=0);
I = sort(I0,1,'descend');
Areshaped=zeros(size(At));
Areshaped(I)=At(I0);
Areshaped=Areshaped(any(Areshaped,2),:)'
その他の回答 (1 件)
Arif Hoq
2022 年 3 月 23 日
編集済み: Arif Hoq
2022 年 3 月 23 日
try this:
A =[0 1 0 1 0
1 0 1 1 0
0 1 0 1 1
1 1 1 0 1
0 0 1 1 0];
output=sort(A,2,'descend');
output(:,end)=[]
4 件のコメント
Arif Hoq
2022 年 3 月 23 日
or try this:
A =[0 -1 0 0 0 0 -2 -2 0 0 0 0 0 2
-1 0 0 0 0 0 0 0 2 0 0 0 2 2
0 0 0 -1 2 -1 0 0 0 0 0 0 0 -2
0 0 -1 0 2 0 0 0 0 0 0 0 0 0
0 0 2 2 0 0 0 0 0 0 0 0 1 2
0 0 -1 0 0 0 -1 0 0 2 0 0 0 -2
-2 0 0 0 0 -1 0 -1 0 2 0 0 0 0
-2 0 0 0 0 0 -1 0 -1 0 2 0 0 0
0 2 0 0 0 0 0 -1 0 0 2 0 -1 0
0 0 0 0 0 2 2 0 0 0 -1 2 0 0
0 0 0 0 0 0 0 2 2 -1 0 2 0 0
0 0 0 0 0 0 0 0 0 2 2 0 -1 2
0 2 0 0 1 0 0 0 -1 0 0 -1 0 0
2 2 -2 0 2 -2 0 0 0 0 0 2 0 0];
b= size(A, 1);
[~, Y] = sort(A == 0, 2);
output= A((1:b).' + (Y - 1) * b);
output( :, ~any(output,1) ) = []
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!