MATLAB Answers

Imaginary Number Notation/ Formatting Real and Complex Results

조회 수: 4(최근 30일)
Hello,
I am performing an eigenvalue calculation for a range of 10 numbers. One of these numbers are negative which results in a complex eigenvalue but the remainder are real numbers. I want to display these to the screen as results, however, when I do this the notation for the real values solutions occur as, for example, "3.464+0i" when it should just be "3.464". My 9th element in the calculation appears as how I want it "0+6.0598e-09i" and I would like to keep it this way. Is there a solution to this or do I just have to deal with the formatting?
Thanks,
Kyle

  댓글 수: 0

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

채택된 답변

Walter Roberson
Walter Roberson 19 Oct 2020
An exact 0 imaginary part shows up as +0i if it is being displayed as part of a matrix of values in which some are not complex, but is dropped if all of the values being displayed are real-valued.
>> A=[3+0i,4+1i]
A =
3.0000 + 0.0000i 4.0000 + 1.0000i
>> A(1), A(2)
ans =
3
ans =
4.0000 + 1.0000i
There is no control over this for automatic display. There are also no sprintf() or fprintf() or compose() format specifiers that display complex values.
You can do things like
Afmt = arrayfun(@num2str, A, 'uniform', 0);
which will get out a cell array of character vectors, which you could then arrange for output, such as by using
fmt = [repmat('%20s ', 1, size(A,2)-1), '%20s\n']; %adjust the 20 to suit
Afmttranspose = Afmt.';
fprintf(fmt, Afmttranspose{:})

  댓글 수: 0

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

추가 답변(1개)

madhan ravi
madhan ravi 19 Oct 2020
편집: madhan ravi 19 Oct 2020
The imaginary part might not be exactly zero.
vpa(Answer)

  댓글 수: 2

Kyle McLaughlin
Kyle McLaughlin 19 Oct 2020
Hi, thank you for your reply...
Just tried that and I have confirmed that all solutions excluding the 9th are real. When I do this command, however, it shows a bunch of digits, is there a way to limit this? I am not familiar with the vpa command, can I do format short to fix it? If i do vpa of the soln that was 3.464 as mentioned previously, I get 3.4641016151377545870548926830117 and when I do that for the complex solution I get 0.0000000060598413313113173933178691931908i.
Thanks
Kyle McLaughlin
Kyle McLaughlin 19 Oct 2020
Update: I have found that if I call for example digits(5) it will show me 5 digits which is great, but now my issue is when I call this variable to be displayed in a table, the table is displayed with [1x1 sym] instead of the actual value.

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

Community Treasure Hunt

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

Start Hunting!

Translated by