Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Matt J
Matt J le 23 Mar 2022
Modifié(e) : Matt J le 23 Mar 2022
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

Plus de réponses (1)

Arif Hoq
Arif Hoq le 23 Mar 2022
Modifié(e) : Arif Hoq le 23 Mar 2022
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 commentaires
Arif Hoq
Arif Hoq le 23 Mar 2022
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by