How to create a symmetrical matrix?
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Plus de réponses (2)
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
le 26 Jan 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 commentaire
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}
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)
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!