How to symmetrize a matrix
Ältere Kommentare anzeigen
Hey I want to write a code that will symmerize a non-symetric matrix by adding zeros here is code I tried writing :
[ny,nx]=size(M); % current Matrix's dimensions
if nx~=ny % Comparing the dimensions
if nx>ny %
ny=nx; %
elseif ny>nx %
nx=ny; %
end %
Z=zeros(ny,nx); % Creating Zero Matrix
M=Z+M; % Adding zeros
[nx,ny]=size(M); % current Matrix's dimentions
end
this doesn't work because of Z and M are not the same size ... any ideas ?
Antworten (2)
Andrei Bobrov
am 28 Mai 2015
Bearbeitet: Andrei Bobrov
am 29 Mai 2015
[ny,nx]=size(M);
if ny~=nx
m = max(ny,nx);
M(m,m)=0;
end
2 Kommentare
Walter Roberson
am 28 Mai 2015
This will not work properly if the matrix is already square, as it will overwrite the bottom-right corner with 0.
Andrei Bobrov
am 29 Mai 2015
Bearbeitet: Andrei Bobrov
am 29 Mai 2015
Hi Walter! You're right.
the cyclist
am 28 Mai 2015
Andrei's method is probably the most elegant. Here is another method:
[ny,nx]=size(M);
Z = zeros(max(ny,nx));
Z(1:ny,1:nx) = M;
Be careful using the word "symmetrize" for this procedure. That term has a specific meaning in matrix algebra.
Kategorien
Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!