Who can define a complex matrix in MATLAB? Pleale translate the FORTRAN into Matlab code?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
In FORTRAN the function COMPLEX can define a complex matrix ,like as COMPLEX B(0:20,0:10). But in MATLAB, there is no COMPLEX function. I have a FORTRAN code. Can you translate it into Matlab code?
PROGRAM MAIN
DOUBLEPRECISION A(0:20,0:10)
COMPLEX B(0:20,0:10)
DO I=0,20
DO J=0,10
A(I,J)=2*I+J
B(I,J)=CMPLX(A(I,J),0.0)
ENDDO
ENDDO
! ! ! Output calculation results: A and B ! ! !
OPEN(1,FILE='A.DAT')
WRITE(1,10) ((A(I,J),J=0,10),I=0,20)
CLOSE(1)
OPEN(2,FILE='B.DAT')
WRITE(2,20) ((B(I,J),J=0,10),I=0,20)
CLOSE(2)
10 FORMAT(X,11F14.8)
20 format( 22f14.8 )
END
2 comentarios
per isakson
el 12 de Oct. de 2017
Editada: per isakson
el 12 de Oct. de 2017
"But in MATLAB, there is no COMPLEX function." See Complex Numbers and complex, Create complex array
Respuestas (2)
KSSV
el 12 de Oct. de 2017
In matlab default class is double.....you need not to initialize as you do in FORTRAN. If you want to define a complex number, you do the following:
a = 5 ;
b = 6 ;
z = a+1i*b ; % complex number
real(z) % gives real part
imag(z) % gives complex part
Same is the case with arrays.
4 comentarios
per isakson
el 12 de Oct. de 2017
Editada: per isakson
el 12 de Oct. de 2017
>> B = 1 + 0i;
>> class(B)
ans =
double
>> imag(B)
ans =
0
and
>> b = complex( 1, 0 )
b =
1.0000 + 0.0000i
>> class(b)
ans =
double
>> b1 = 1;
>> imag(b1)
ans =
0
Walter Roberson
el 12 de Oct. de 2017
In MATLAB, if all of the complex entries in the matrix are 0, then MATLAB removes the complex portion, leaving it real-only valued. However, in nearly all cases MATLAB treates real-only valued matrices as being equivalent to a complex matrix whose complex parts all read out as zero. In the above code, real(B) would extract the real component and imag(B) would extract the imaginary component which would come out all zero if B happened to be a real-only value.
James Tursa
el 12 de Oct. de 2017
Editada: James Tursa
el 12 de Oct. de 2017
A = bsxfun(@plus,2*(0:20)',(0:10));
B = complex(A);
Same resulting matrices as Fortran, except that MATLAB indexing is always 1-based. I.e., the imaginary part of B is physically present in memory, same as Fortran, even though the imaginary values are identically zero.
0 comentarios
Ver también
Categorías
Más información sobre 从 Fortran 调用 MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!