How to create a symmetrical matrix?

150 visualizaciones (últimos 30 días)
ric1321
ric1321 el 3 de Feb. de 2017
Editada: Stephen23 el 27 de En. de 2023
Hi, I have to fill a matrix with some data that come from a subroutine. The matrix is symmetrical over the two diagonals, so I have to build just a quarter of it and then to copy one the other parts. How can I do?
  1 comentario
Stephen23
Stephen23 el 3 de Feb. de 2017
Editada: Stephen23 el 3 de Feb. de 2017
@ric1321: what happens along the diagonals? Are the diagonal values constant?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 3 de Feb. de 2017
Editada: Stephen23 el 3 de Feb. de 2017
I looked at this task, and tried several possibilities to convert this:
>> M = [1,2,3,4,5;0,6,7,8,0;0,0,9,0,0;0,0,0,0,0;0,0,0,0,0]
M =
1 2 3 4 5
0 6 7 8 0
0 0 9 0 0
0 0 0 0 0
0 0 0 0 0
But then it occurred to me: the question states "I have to fill a matrix with some data that come from a subroutine", so perhaps the simplest way is to avoid it all together: if you have to fill the matrix with these values (presumably in a loop and using some indexing), then instead of just filling one value, fill in all four values at once. This is likely going to be simpler than any method of taking that quarter matrix, rotating, and merging again...
So the simplest way for your situation might be to not "copy and rotate", but to allocate each new value to four locations when you put it into the matrix:
S = size(M);
[R,C] = find(M);
Z = nan(S);
for k = 1:numel(R) % for each subroutine value
x = [R(k),1+S(1)-R(k),C(k),1+S(2)-C(k)]; % index
m = M(R(k),C(k)); % new subroutine value here
Z(x,x) = m;
end
which gives this:
Z =
5 4 3 4 5
4 8 7 8 4
3 7 9 7 3
4 8 7 8 4
5 4 3 4 5
Or you can use the method exactly as I did here: to copy the elements of that quarter to the other quarters.

Más respuestas (2)

Guillaume
Guillaume el 3 de Feb. de 2017
Editada: Guillaume el 3 de Feb. de 2017
One of the many ways to do this:
v = [1 2 3 4; 0 5 6 7; 0 0 8 9; 0 0 0 10];
tv = v';
v(tril(true(size(v)))) = tv(tril(true(size(v))))

Benjamin Klassen
Benjamin Klassen el 26 de En. de 2023
Symmetric Matrix Generator
% Rows
r1=[1,2,3,4,5,6,7,8];
r2=[5,2,6,7,9,8,5];
r3=[1,4,6,2,8,6];
r4=[1,6,2,9,4];
r5=[1,3,2,7];
r6=[1,7,9];
r7=[6,2];
r8=[9];
nr=length(r1); % Number of rows
K=zeros(nr,nr);
% Symmetric Matrix Assembly
for i=1:nr
d=join(['r',num2str(i)])
rowvalues=eval(d);
K(i,i:end)=rowvalues;
K(i:end,i)=rowvalues;
end
  1 comentario
Stephen23
Stephen23 el 27 de En. de 2023
Editada: Stephen23 el 27 de En. de 2023
This would be much better without the evil EVAL call and anti-pattern numbered variable names:
C = {[1,2,3,4,5,6,7,8];[5,2,6,7,9,8,5];[1,4,6,2,8,6];[1,6,2,9,4];[1,3,2,7];[1,7,9];[6,2];9}
C = 8×1 cell array
{[1 2 3 4 5 6 7 8]} {[ 5 2 6 7 9 8 5]} {[ 1 4 6 2 8 6]} {[ 1 6 2 9 4]} {[ 1 3 2 7]} {[ 1 7 9]} {[ 6 2]} {[ 9]}
N = numel(C);
M = zeros(N,N);
for k = 1:N
V = C{k};
M(k,k:end) = V;
M(k:end,k) = V;
end
display(M)
M = 8×8
1 2 3 4 5 6 7 8 2 5 2 6 7 9 8 5 3 2 1 4 6 2 8 6 4 6 4 1 6 2 9 4 5 7 6 6 1 3 2 7 6 9 2 2 3 1 7 9 7 8 8 9 2 7 6 2 8 5 6 4 7 9 2 9

Iniciar sesión para comentar.

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by