Matlab/Octave addition, losing digits of precision - matlab

In Matlab/octave, when I add two numbers, I am losing some of my digits.
>>> 23.0 + 0.65850
ans = 23.659
How do I get back a double that is 23.65850?

The number is being rounded only for display purposes. Take a look at the format command if you wish to change it.
octave> 23 + 0.65850
ans = 23.659
octave> format free
octave> 23 + 0.65850
ans = 23.7
octave> format long g
octave> 23 + 0.65850
ans = 23.6585
Take a look at help format for the other options but remember, that this only affects the display. You are not losing any precision.

Related

[Octave]Trouble visualising how to write a row vector of exponents

as the title states, I was following a guideline on coding a polynomial regression function but I am currently stuck on what it means to write a row vector of exponents. I need to initialise two variables, one being 'vector1', a column vector of a variable 'X', and 'vector2' which is meant to be a row vector of exponents from 1 to 'p'. Once that's done, I'm supposed to fill it in the bsxfun as such "X_poly = bsxfun(#power, vector1, vector2)".
Now the problem arises when I try to write in vector2. I have trouble visualising how to write this code. I've tried "vector2 = X(1:p,:)", "vector2 = X*p", "vector2 = X'(1:p,:)". Obviously none of these worked and I just feel this strong sense of defeat everytime I get it wrong. I've tried googling but the results have yielded no fruition.
I feel very lost and I'm grasping at straws at this point.
You don't need to use bsxfun here, the power function (and its equivalent operator .^) is vectorised (i.e. it can accept arrays process them in an element-wise manner).
octave:1> v1 = 1:10;
octave:2> v2 = 1:10;
octave:3> v1 .^ v2
ans =
1 4 27 256 3125 46656 8.2354e+05 1.6777e+07 3.8742e+08 1e+10
octave:4> power(v1,v2)
ans =
1 4 27 256 3125 46656 8.2354e+05 1.6777e+07 3.8742e+08 1e+10
octave:5> bsxfun(#power, v1, v2)
ans =
1 4 27 256 3125 46656 8.2354e+05 1.6777e+07 3.8742e+08 1e+10

Add 1 to the least significant digit of a number in MATLAB

Example: 6.321: I need it to be 6.322.
5.14875: I need it to be 5.14876.
How can I do this?
If you represent numbers as floating point or double precision floating point, this problem is a disaster.
If you can read in a number as a string (you mentioned get the number with the input command), you could do:
x = input('ENTER A NUMBER: ','s');
decimal_place = find(fliplr(x)=='.',1) - 1;
x_val = str2double(x);
if(~isempty(decimal_place))
y = x_val + 10 ^ -decimal_place;
else % if there is no decimal place, find first non-zero digit to get sigfig
warning('ambiguous number of significant digits');
first_nonzero_digit = find(fliplr(x)~='0',1);
if(~isempty(first_nonzero_digit))
y = x_val + 10 ^ (first_nonzero_digit - 1);
else
y = x_val + 1;
end
end
disp('your new number is');
disp(y);
Example test runs:
ENTER A NUMBER: 1.9
your new number is
2
ENTER A NUMBER: 3510
your new number is
3520
ENTER A NUMBER: 323.4374
your number is
323.4375
ENTER A NUMBER: 0
your number is
1
#AndrasDeak - I think you're right the first time. The hard part is not the rounding - it's defining the "last" decimal.
Since floating point numbers aren't exact, I can't think of a reliable way to find that "last" decimal place - in any language.
There is a very hacky way that comes to mind, though. You could "print" the number to a string, with 31 numbers after the decimal point, then working right from the dot, find the first place with 15 0s. (Since double precision numbers can only stably represent the first 14 decimal places and you get a 15th that varies, 31 decimal place will ALWAYS give you at least 15 0s after the last sig digit.)
>> a = 1.34568700030041234556
a =
1.3457
>> str = sprintf('%1.31', a)
str =
Empty string: 1-by-0
>> str = sprintf('%1.31f', a)
str =
1.3456870003004124000000000000000
>> idx = strfind(str, '000000000000000')
idx =
19
>> b = a*10^(idx(1)-3)
b =
1.3457e+16
>> sprintf('%10.20f', b)
ans =
13456870003004124.00000000000000000000
>> c = b+1
c =
1.3457e+16
>> sprintf('%10.20f', c)
ans =
13456870003004124.00000000000000000000
>> final = floor(c)/10^(idx(1)-3)
final =
1.3457
>> sprintf('%10.31f', final)
ans =
1.3456870003004124000000000000000
I think that's a relatively reliable implementation.
http://www.mathworks.com/matlabcentral/answers/142819-how-to-find-number-of-significant-figures-in-a-decimal-number
assuming your just trying to do regular rounding:
i'd use the round function built into matlab.
let's do your example above..
5.14875 has 5 decimal places and you want it to be converterd to 5.14876.
Lets assume you that the 6th decimal place was 9 (so your number is 5.148759)
%Step 1:changethe format so that your going to be able to see all of the
%decimal places
format long
%step2:now enter the original number
OriginalNumber=5.148755
%step 3 take the original number and round it to your new number
NewNumber=round(OriginalNumber,5)
this solution will not work if the 6th number (that you did not show) was a <5 because the computer will not know to round up
assuming your just trying to cut the numbers off...
You cannot do this in regular default matlab floating point numbers. To keep my explination simple I'll just state that without an explination. I'd do some review on the different ways matlab stores # (int vs floating point) on the matlab website. They have excellent documentation.

undefined result during formatting in hexadecimal form

I can't understand what is reason of following result
>> format hex
>> 10
ans =
4024000000000000
>> 12
ans =
4028000000000000
as i know this numbers should be written in hexadecimal format,but why such result?i have tried different variant for example like this
>> x=20;
>> format hex
>> x
x =
4034000000000000
if i will try different format
>> format long
>> x=10
x =
10
>> x=10.456
x =
10.456000000000000
it works nice, so what is problem?
Matlab is behaving absolutely correct, x=12 creates a 64 bit floating point number witch has the presented hexadecimal representation. What you probably want is:
>>uint32(12)
ans =
0000000c

How to convert any decimal numbers to its next integer in matlab?

I know that there are two function 'fix' and 'round' which can convert decimal to integer. However, I cant find a way to round off any decimal number to its next integer. Anyone know how this can be done.
For instance, I want to convert 1.1 to 2, 3.01 to 4, 4.009 to 5 or any decimal numbers.
Thank you.
That's the ceiling function, matlab ceil:
>> ceil(1.1)
ans =
2
>> ceil(3.01)
ans =
4
>> ceil(4.009)
ans =
5

How to use matrix from answer further?

If I write a random matrix (A) and get results:
ans = 1 2 3 4 %next row 5 6 7 8
how can I get it written in this form:
A = [1,2,3,4;5,6,7,8]; ?
(Of course I want to avoid retyping or copy-pasting it)
If I understand your question correctly, mat2str is what you are looking for. Although it won't use commas, but spaces, and overwrite ans (i.e. ans will be of type char afterwards).
Example (the second argument limits the number of digits):
>> rand(2,3); mat2str(ans,2)
ans =
[0.42 0.79 0.66;0.92 0.96 0.036]
The last answer that you calculated is saved in a special variable named ans. Simply assign that value to A.
% some calculations
[1,2,3,4;5,6,7,8]
% assign to A
A = ans;