Who can define a complex matrix in MATLAB? Pleale translate the FORTRAN into Matlab code?

4 visualizaciones (últimos 30 días)
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

Respuestas (2)

KSSV
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
per isakson el 12 de Oct. de 2017
Editada: per isakson el 12 de Oct. de 2017
"same result" I guess that depends on how you define "result". See See the comment by John D'Errico
>> 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
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.

Iniciar sesión para comentar.


James Tursa
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.

Categorías

Más información sobre 从 Fortran 调用 MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!