How to symmetrize a matrix

조회 수: 16 (최근 30일)
Ross Dubilet
Ross Dubilet 2015년 5월 28일
편집: Andrei Bobrov 2015년 5월 29일
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 ?

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2015년 5월 28일
편집: Andrei Bobrov 2015년 5월 29일
[ny,nx]=size(M);
if ny~=nx
m = max(ny,nx);
M(m,m)=0;
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 5월 28일
This will not work properly if the matrix is already square, as it will overwrite the bottom-right corner with 0.
Andrei Bobrov
Andrei Bobrov 2015년 5월 29일
편집: Andrei Bobrov 2015년 5월 29일
Hi Walter! You're right.

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2015년 5월 28일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by