How can I create a row of numbers form 1 to 10 ?. like this [1.0000, 2.0000, 3.0000,...]?
301 views (last 30 days)
Show older comments
Chinonso Collins Unaichi
on 3 May 2016
Commented: Chinonso Collins Unaichi
on 3 May 2016
Hallo guys, I am so new to matlab, and i know this is a silly question. How can I create a row of numbers form 1 to 10. like this [1.0000, 2.0000, 3.0000,...] with intervals of 1.0000? if I do [1:1:10], I get [1,2,3,..] but I want it like this[1.0000,2.0000,3.0000] kindly help! Thanks in advance.
0 Comments
Accepted Answer
John BG
on 3 May 2016
with
format long;x=1:10
=
1 2 3 4 5 6 7 8 9 10
vpa only adds one zero:
y=vpa(x,5)
=
[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
y=vpa(x,10)
=
[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
floating point boundaries (without vpa):
flintmax
=
9.007199254740992e+15
intmax
=
2147483647
intmin
=
-2147483648
with fixed point precision objects, details here: http://uk.mathworks.com/help/fixedpoint/ref/fi.html?searchHighlight=fi
you get more than 4 zeros:
a = fi(pi, 1, 8, 3)
a =
3.125000000000000
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 3
but only if it is rounding non-zero decimals:
a = fi(1, 1, 8, 3)
=
1
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 3
a = fi(1.0000, 1, 8, 3)
=
1
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 3
tried 1.0001 and then -0001 but did not work:
b = fi(1.0001, 1) b = 1.000122070312500
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 14
b-.0001
Warning: The behavior of adding or subtracting a fi and a non-fi operand has
changed. The non-fi operand is now cast to the data type of the fi operand.
See the release notes for information on retaining the old behavior of best
precision scaling cast. In - (line 37)
=
1
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 17
FractionLength: 14
c=b-.0001 = 1
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 17
FractionLength: 14
b2=fi(0.0001,1)
=
1.000016927719116e-04
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 16
FractionLength: 28
b-b2 = 1.000022068619728
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 31
FractionLength: 28
d=b-b2 = 1.000022068619728
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 31
FractionLength: 28
d2=vpa(d,4)
=
1.0
If you really want to present the vector exactly with 4 zero decimals, you can translate each figure to char,
x=[1:1:4]
x_char=char(x+48)'
=
1
2
3
4
and then add '.000'
x2=repmat(['.0000'],4,1)
=
.0000
.0000
.0000
.0000
x_char=[x_char x2]
=
1.0000
2.0000
3.0000
4.0000
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John
More Answers (1)
Alessandro Masullo
on 3 May 2016
Matlab deals with double variables by default. If you write 1, it is actually 1.00000000
If you just want to display it, you can use sprintf('%.5f',1).
To check your variable type, you can use whos
2 Comments
Guillaume
on 3 May 2016
Edited: Guillaume
on 3 May 2016
To add to Alessandro's answer. You're confusing two different things, the number that is stored in the matrix and the way it is displayed.
In matlab and in math, there is no difference whatsoever between 1 and 1.0000. You can change the way matlab displays number in the command window with format. Most of the formats display integer without any decimal, only the engineering format always use decimal:
>>format shorteng
>>1
ans =
1.0000e+000
Or as Alessandro said, you can use the format string of sprintf to output your number to the command line.
But as said, it is just cosmetics. The numbers are the same regardless of how you display them.
Alessandro Masullo
on 3 May 2016
Yes, you're right. The only real difference comes when you specify the variable type:
K>> format shorteng
K>> double(1:5)
ans =
1.0000e+000 2.0000e+000 3.0000e+000 4.0000e+000 5.0000e+000
K>> uint8(1:5)
ans =
1 2 3 4 5
See Also
Categories
Find more on Fixed-Point Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!