calculations with respect to row vectors

4 views (last 30 days)
Dear Sir:
I was using an old version of matlab (I believe matlab 6) at school.
Can you please tell me why matlab is returning data that appears to be in error?
Please examine the *.m code that I have copied and pasted below (delete or insert the semicolon symbol ; to see the values output by matlab):
%Example:
t=[0:5] % t = [ 0 1 2 3 4 5 ]
t=[0:5]/5 % t = [ 0 0.2000 0.4000 0.6000 0.8000 1.0000 ]
% so far, so good everything is logical and rational
% The problem is set forth in the next line below:
t = [0:16383]/125000000 % Column 16384 = 0.13106400000000; This value is not logical
% Verification that a mathematical anomoly is present:
a=16383/125000000 % matlab returns: a = 1.310640000000000e-004
Can you tell me why matlab is returning this value? Also, try this equation below and the anomaly will repeat itself.
b=16383/125000000000;

Accepted Answer

Star Strider
Star Strider on 16 Jul 2016
It probably has to do with the existing format:
format long
t = [0:1638:16383]/125000000
t =
1.0e-03 *
Columns 1 through 4
0 0.013104000000000 0.026208000000000 0.039312000000000
Columns 5 through 8
0.052416000000000 0.065520000000000 0.078624000000000 0.091728000000000
Columns 9 through 11
0.104832000000000 0.117936000000000 0.131040000000000
Note that the display of the vector (that I shortened for convenience) is preceded by ‘1.0e-03 *’ indicating that the entire vector is to be multiplied by 1e-03.
So there is no error. You likely didn’t see the common multiplier (displayed at the beginning of the output) when you output the very long, 16384-element vector, because it scrolled off the top of the Command Window before you could see it. Shortening the vector for illustration purposes displays it.
  4 Comments
loginID00
loginID00 on 17 Jul 2016
I think the best way to solve this problem may be to pose this question:
Do you know a way I can configure the command window outputs so that I always return a simple, numeric, whereby the complete answer is contained in every vector/array entry?
Star Strider
Star Strider on 17 Jul 2016
I would use fprintf (or sprintf) to do that. See the documentation and the variety of format descriptors (such as '%f', '%E', and the rest) for details. You will have to experiment to get the result you want.

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!