Visualization of a complex number when the imaginary part is zero in MATLAB

In MATLAB R2012:

>> rand(3) ; ans(1,1)=1+i ans = 1.0000 + 1.0000i 0.5060 0.9593 0.7513 0.6991 0.5472 0.2551 0.8909 0.1386 

But in R2013:

 >> rand(3) ; ans(1,1)=1+i ans = 1.0000 + 1.0000i 0.9134 + 0.0000i 0.2785 + 0.0000i 0.9058 + 0.0000i 0.6324 + 0.0000i 0.5469 + 0.0000i 0.1270 + 0.0000i 0.0975 + 0.0000i 0.9575 + 0.0000i 

How can i fix this?

Sincerely.

+6
source share
2 answers

Without the opportunity to try, I can only guess that you can play with the format.

The best would be format shortg , this can hide the imaginary part or just make it less distracting:

 rand(3) ; ans(1,1)=1+i format shortg rand(3) ; ans(1,1)=1+i 

Yes, this is shortg instead of short . He tries not to show irrelevant zeros and decimals.

+1
source

The best way to write this would be as a new feature that simply displays in the format you like. Example:

 function [ out ] = new_display( in ) for i=1:size(in,1) for j=1:size(in,2) fprintf('%.4f', real(in(i,j))); if(imag(in(i,j)))>0 fprintf(' + %.4fi\t', imag(in(i,j))); else fprintf('\t\t\t'); end end fprintf('\n'); end 

gives:

 >> new_display(ans) 0.8147 + 1.0000j 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 

Usually, if you just type ans , matlab calls the display () function. You can overload this function yourself, but MathWorks says this is a bad idea (I agree).

Link:

0
source

Source: https://habr.com/ru/post/954445/


All Articles