简体   繁体   中英

Complex number visualisation when imaginary part is null 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 it?

Regards.

Without having the option to try, I can only guess that you may want to play around with the format.

My best bet would be format shortg , it may hide the imaginary part or simply make it less distracting:

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

Yes that is shortg instead of short . It tries not to show irrelevant zeros and decimals.

The best way to write this would be as a new function that simply outputs in the format you please. 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  

Normally, if you just type ans , matlab calls the display() function. You could overload this function with your own, but MathWorks says that it is a bad idea (I agree).

Ref:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM