Who can define a complex matrix in MATLAB? Pleale translate the FORTRAN into Matlab code?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
per isakson
le 12 Oct 2017
Modifié(e) : per isakson
le 12 Oct 2017
"But in MATLAB, there is no COMPLEX function." See Complex Numbers and complex, Create complex array
Réponses (2)
KSSV
le 12 Oct 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 commentaires
per isakson
le 12 Oct 2017
Modifié(e) : per isakson
le 12 Oct 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
le 12 Oct 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
le 12 Oct 2017
Modifié(e) : James Tursa
le 12 Oct 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 commentaires
Voir également
Catégories
En savoir plus sur 从 Fortran 调用 MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!