필터 지우기
필터 지우기

mat2str() on complex values with 0 inaginary part

조회 수: 5 (최근 30일)
Firan Lucian
Firan Lucian 2021년 1월 26일
편집: Jan 2021년 1월 26일
Hi,
Tried mat2str(complex(5,6)) witch seems ok:
ans =
'5+6i'
But other complex values with imaginary part 0 or 0+0i are printed as real values:
mat2str(complex(5,0))
ans =
'5'
mat2str(0+0i)
Seems incorect to me, is this a bug ?

채택된 답변

Jan
Jan 2021년 1월 26일
편집: Jan 2021년 1월 26일
Mathematically 5 is the same as 5+0i, so the output is correct.
So omitting the imaginary part is a question of taste.
If you want to display it, do this expliticitly:
x = 1 + 2i;
sprintf('%g %gi', real(x), imag(x))
Alternatively you can copy the code of mat2str to e.g. mat2stri anywhere in your user-defined folders in the path and remove the line:
imagStr(imagVal == 0) = "";

추가 답변 (2개)

Firan Lucian
Firan Lucian 2021년 1월 26일
~isreal(complex(5,0))
ans =
logical
1
~isreal(5)
ans =
logical
0

Steven Lord
Steven Lord 2021년 1월 26일
Not a bug. Many operations in MATLAB, if the result has an all zero imaginary part, will remove that imaginary part. complex is one of the few operations that does not by design.
x = complex(5, 0)
x = 5.0000 + 0.0000i
y = x(1)
y = 5
z = x + 0
z = 5
  댓글 수: 1
Firan Lucian
Firan Lucian 2021년 1월 26일
I don't like this output inconsistency, as this functions may provide some human readable string; that should contain, not hide, the complex storage, but this is my opinion…
(maybe some extra options could help ...,'complex', true)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by