필터 지우기
필터 지우기

How to calculate diagonal for NxN matrix ?

조회 수: 1 (최근 30일)
Lily
Lily 2012년 9월 18일
Hi
I'm trying to calculate the sum diagonal from a square matrix (size NxN). For example: A = [1 2 3 4; 5 6 7 8; 9 0 1 2; 3 4 5 6]; %square matrix Then if you would rotate the matrix by 45 degrees and take sum of the rotated martix. Like: imrotate(A,45)
What I'm trying to do is to find the diagonal sum of A. Therefore I want to construct a vector of size 1x(N+2) or for this A = [1 2 3 4; 5 6 7 8; 9 0 1 2; 3 4 5 6] it would be an vector of size 1x6.
Could you please help me? :)
  댓글 수: 4
Jan
Jan 2012년 9월 18일
Do you mean 90 deg instead of 45 deg?
Honglei Chen
Honglei Chen 2012년 9월 18일
I don't quite get it. Are you saying you want the sum along each diagonal? But if so, why is the resulting vector 1x(N+2)? Shouldn't it be 1x(2*N-1)? Could you post the desired answer for your sample matrix?

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

채택된 답변

Thomas
Thomas 2012년 9월 18일
YOu could use
sum(diag(A))
this is essentially
trace(A)
What does rotation by 45 mean
  댓글 수: 2
Lily
Lily 2012년 9월 18일
편집: Lily 2012년 9월 18일
Like if I would imrotate(A,45)
Thomas
Thomas 2012년 9월 18일
IF you use imrotate(A,45) you get a 9x9 matrix and not the 4x4 matrix you started with..

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

추가 답변 (1개)

Wayne King
Wayne King 2012년 9월 18일
편집: Wayne King 2012년 9월 18일
Just use trace()
trace(A)
  댓글 수: 2
Lily
Lily 2012년 9월 18일
Thx for the answer, but f I use trace(A) I only get the diagonal of the matrix A.
Wayne King
Wayne King 2012년 9월 18일
It's easy to say in general what an NxN matrix looks like for a multiple of 90 degrees, but what does it look like for 45? For N>3, I'm not sure what that looks like without padding the matrix with zeros like imrotate

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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by