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
Related
i need to quantize and encode an input signal using matlab so i will use uencode function . The problem is that i am confused about its process , the description says that it quantize and encode the input as integer and then he has displayed an example :
u = -1:0.01:1;
y = uencode(u,3);
plot(u,y,'.')
The output is just integers , can somebody just explain what this integers exactly are ?? and if i need the binary codes of the input u what i must do to get them ?
uencode takes the range of floating point numbers between -1.0 and 1.0, and maps it to the integers from 0 to (2^n)-1.
For example, with n=8, the possible integers are 0 to 255. -1.0 gets mapped to 0, +1.0 gets mapped to 255, and all decimal values in between get mapped to the closest integer.
In the code example you gave, n=3, so it is mapping to the integers 0 to 7. The plot shows horizontal lines because with so few integers available to map to, many floating point values map to the same integer.
To convert a base 10 integer to a base 2 binary string, use the function dec2bin.
>> dec2bin(5)
ans =
101
>> dec2bin(17)
ans =
10001
If you wanting leading zeros, say so that they are always 8 bits long, use the minimum length as a second argument:
>> dec2bin(5, 8)
ans =
00000101
I have a vector of doubles and I want to see what are the exact numbers inside the vector I get in format long.
1.0e+03 *
-0.002202883146567
1.182072110137121
-0.002242966651629
-0.000584787748712
0.022251505213305
0.037460846794487
Can I make some adjusment so that I can directly see the number, rounded to let's say the 5th or 6th element after the decimal point, whenever I type the name of the variable?
fprintf('%.6f\n', 0.037460846794487)
It'll round 0.037460846794487 to 6 decimal places as shown:
>> fprintf('%.6f\n', 0.037460846794487)
0.037461
Or you can also use sprintf('%.6f\n', 0.037460846794487) , particularly, if you want to save the rounded off output in a variable.
>> a=sprintf('%.6f\n', 0.037460846794487)
a =
0.037461
and for the matrix you mentioned, you can make the following adjustment:
%Your matrix
A = 1.0e+03 * [ -0.002202883146567 ;
1.182072110137121 ;
-0.002242966651629 ;
-0.000584787748712 ;
0.022251505213305 ;
0.037460846794487 ];
A = sprintf('%.6f\n', A) %Adjusted to 6 decimal digits
So i'm creating a function that is trying to compare decimal numbers with scientific notation. I am trying to round them off and saving them into new variables.
What I did is this...
>> digits(3);
>> Y = vpa(0.000036856864)
Y =
3.69e-5
>> Z = vpa(0.000036857009)
Z =
3.69e-5
>> eq(Y,Z)
ans =
0
Technically the new rounded decimal is saved unto Y and Z, so when I compare the two variables, it gives me 0, but it should be 1. How can I fix this to make sure that the answer equals to 1?
Any help is appreciated!
The values returned by vpa aren't actual numbers - they're symbolic objects that still contain the original value of the number (before rounding). To compare the two, you should convert them back to double:
Y = double(vpa(0.000036856864))
Z = double(vpa(0.000036857009))
eq(Y,Z)
which should return 1
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.
I'm working in Matlab using Non-negative Matrix factorization to decompose a matrix into two factors. Using this I get from A two double precision floating point matrices, B and C.
sample results are
B(1,1) = 0.118
C(1,1) = 112.035
I am now trying to modify specific bits within these values but using the bitset function on either values I get an error because bitset requires unsigned integers.
I have also tried using dec2bin function, which I assumed would convert decimals to binary but it returns '0' for B(1,1).
Does anyone know of any way to deal with floats at bit level without losing precision?
You should look into the typecast and bitset functions. (Doc here and here respectively). That lets you do stuff like
xb = typecast( 1.0, 'uint64' );
xb = bitset( xb, 10, 1 );
typecast( xb, 'double' );
The num2hex and hex2num functions are your friends. (Though not necessarily very good friends; hexadecimal strings aren't the best imaginable form for working on binary floating-point numbers. You could split them into, say, 8-nybble chunks and convert each to an integer.)
From the MATLAB docs:
num2hex([1 0 0.1 -pi Inf NaN])
returns
ans =
3ff0000000000000
0000000000000000
3fb999999999999a
c00921fb54442d18
7ff0000000000000
fff8000000000000
and
num2hex(single([1 0 0.1 -pi Inf NaN]))
returns
ans =
3f800000
00000000
3dcccccd
c0490fdb
7f800000
ffc00000