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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Matt J
Matt J el 23 de Mzo. de 2022
Editada: Matt J el 23 de Mzo. de 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

Más respuestas (1)

Arif Hoq
Arif Hoq el 23 de Mzo. de 2022
Editada: Arif Hoq el 23 de Mzo. de 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 comentarios
Arif Hoq
Arif Hoq el 23 de Mzo. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by