Changing the number of decimal points for a complex double array

Hello all ,
I am calculating the eigen vectors for a matrix , the output of the function stores the result as a complex double. but the problem is that it saves only to the 4th decimal point and rounds up . for example : instead of -0.99995+0.00000j it is stored as -1.0000+0.0000j . This is a problem as when it tries to save 0.00003+0.00690j as 0.0000+0.0069j. When I display the output matrix in the command window it shows the variable normally as double-precision floating-point values that are 8 bytes (64 bits). but when saving in the workspace , it does the rounding . This can be very critical as it these calcuations are related to sensitivity analysis to power system. The code used is simply as follows:
A=[ -1 0 -990;
0.04714 -0.4762 0;
0 100 -100];
e=eig(A);
[V,D,W]=eig(A);
and the result in the command window as follows:
V =
Columns 1 through 2
-0.999952261875921 + 0.000000000000000i -0.999952261875921 + 0.000000000000000i
0.000027567894396 + 0.006899820799505i 0.000027567894396 - 0.006899820799505i
0.000501495829522 + 0.006900303189568i 0.000501495829522 - 0.006900303189568i
Column 3
0.994990280675026 + 0.000000000000000i
-0.000469071210390 + 0.000000000000000i
0.099970602351055 + 0.000000000000000i
but in the work space saved as follows:
Capture21.JPG
Can anyone please help i how to save that matrix in the same format as it is computed ? .Thanks in advance .

3 Comments

Thanks for your answer , but it didnot solve the problem . I think it just affects the result in the command window not how the variable is stored in the workspace.
No. MATLAB does indeed store a number as a double in more than 4 digits. Format does only change how the number is displayed. But nothing impacts how many digits are stored as a double.
When you look at the number in the workspace browser, that is not in fact how it is stored!

Sign in to comment.

 Accepted Answer

Are you sure rounding is going on?
When you view the workspace, sure it shows you the truncated/rounded version on the screen, but that does not mean it is stored internally that way.
For example if you "ctrl-c" copy the (2,1) element of V from the workspace browser, and then "ctrl-v" paste into the workspace, I get
>> [2.75678943956489e-05 + 0.00689982079950539i]
ans =
0.000027567894396 + 0.006899820799505i
which is accurate to full machine precision. What you see in the workspace is not fully what's stored.

3 Comments

+1, but with the subtle caveat that this is not in fact full machine precision.
MATLAB uses binary to store the bits of your number, and the floating point DECIMAL number displayed is not binary. So when MATLAB displays that number in the command window, you are not seeing full machine precision.
For example, when we create the number 2.75678943956489e-05 as a double, we see it displayed as such. But MATLAB stored it as effectively the binary number:
1.1100111010000011001101000111000011111010001000110010 * 2^-16
We can test that here:
sum(2.^[-16 -17 -18 -21 -22 -23 -25 -31 -32 -35 -36 -38 -42 -43 -44 -49 -50 -51 -52 -53 -55 -59 -63 -64 -67])
ans =
2.75678943956489e-05
Converted to decimal digits, the decimal representation of the number that actually got stored was:
sprintf('%0.70f',2.75678943956489e-05)
ans =
'0.0000275678943956489009713302362758469143955153413116931915283203125000'
But anything at or beyond the point
eps(2.75678943956489e-05)
ans =
3.3881317890172e-21
should be viewed as floating point trash.
So full machine precision is a concept you should be careful to use when discussing decimal numbers.
Note that in the workspace variable browswer, there is a tab called view. In there, we can change the display format used by the browser. This is apparently not controlled by the format command.
Yes, the view tab solved the answer :D Thanks.

Sign in to comment.

More Answers (1)

Go into preferences for workspace and change the default display format. The default format for the workspace viewer is not affected by the "format" command.

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!