필터 지우기
필터 지우기

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