Who can define a complex matrix in MATLAB? Pleale translate the FORTRAN into Matlab code?
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
per isakson
2017 年 10 月 12 日
編集済み: per isakson
2017 年 10 月 12 日
"But in MATLAB, there is no COMPLEX function." See Complex Numbers and complex, Create complex array
回答 (2 件)
KSSV
2017 年 10 月 12 日
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 件のコメント
per isakson
2017 年 10 月 12 日
編集済み: per isakson
2017 年 10 月 12 日
>> 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
2017 年 10 月 12 日
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
2017 年 10 月 12 日
編集済み: James Tursa
2017 年 10 月 12 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で 从 Fortran 调用 MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!