How can I reshape a square matrix to a rectangular matrix based on its adjacency list?

1 次查看(过去 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

采纳的回答

Matt J
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),:)'
Areshaped = 14×6
-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

更多回答(1 个)

Arif Hoq
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)=[]
output = 5×4
1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 0
  4 个评论
Arif Hoq
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) ) = []
output = 14×6
-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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by