Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

diagonalise a matrix

Status
Not open for further replies.

yorkie2010

Programmer
Feb 22, 2010
2
GB
Hi everyone,

I have a matrix A = [1 2 3] and would like to diagonalise it so that it becomes:

A = [1 0 0
0 2 0
0 0 3]

I am Matlab user so it can easily be done by using diag(A). But I need to do it in fortran. Does anyone know how to write it in F95? Many thanks in advanced.

Regards,
Vincent
 
You can write your own simple function or subroutine which takes on input the vector of dimension N (=the matrix diagonal) and returns a diagonal matrix of dimension N x N.
But why should you store the zero elements in memory? It's better to store only the diagonal vector with non zero elements.

 
You can't reshape a 1D array to a 2D array in Fortran. It would have to be something like diag(A, B, N) where A is the 1D array and B is the 2D array. One way, depending on whether your compiler supports matrix operations is
Code:
B = 0
do i = 1, N
   B(i,i) = A(i)
end do
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top