Compare displayed digits of double in Matlab [duplicate] - matlab

This question already has answers here:
What are the best practices for floating-point comparisons in Matlab?
(5 answers)
Closed 8 years ago.
I need to compare two double values in Matlab and check if they are equal. Now the two compared values are displayed to the user, so he can check the printed result, if necessary.
Now I need to know: Is it possible to compare the two double values so that they are equal, if their decimal representation (using 15 significant digits) is equal? For performance reasons, I would prefer not to compare the resulting strings.
For example the two hexadecimal values 3fd04b2bcf617348 and 3fd04b2bcf617359 represent the same displayed number and should therefore be treated equal, whereas 3fd04b2bcf617348 and 3fd04b2bcf617347 have different decimal representations and should be treated as different, even if their difference is lower:
fprintf('eq: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359'))
fprintf('ne: %.15g\n', hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347'))

You can round the values created by hex2num to 15 digits. This is done using the round function.
For MATLAB R2014b and higher, you can specify the precision directly
b = round(a,15);
For older versions, round just rounds to integers, so you will have to do it manually:
b = round(10^15 .* a) ./ 10^15;
If you compare the numbers after rounding, you get the desired behaviour:
a = [ hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617359') ; ...
hex2num('3fd04b2bcf617348'), hex2num('3fd04b2bcf617347') ];
% Round to 15 digits
b = round(10^15 .* a) ./ 10^15;
% Compare results
abs(b(1,1) - b(1,2)) < 4*eps(b(1,1))
ans =
1
abs(b(2,1) - b(2,2)) < 4*eps(b(2,1))
ans =
0

Related

Matlab 2013a index error [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 6 years ago.
I got exactly this error:
Attempted to access E(3,1); index must be a positive integer or logical.
But the index is E(3,1), those numbers are both positive. What is going on?
for t=T:0.2:4
for i=1:N
% D = D +1
x = randi(Nsamples,1,1);
if(x==1)
Etemp = E(t*5,i) - S(x)*S(x+1) + (-S(x))*S(x+1);
elseif(x==Nsamples)
Etemp = E(t*5,i) - S(x)*S(x-1) + (-S(x))*S(x-1);
else
%********************* This is the error line
Etemp = E(t*5,i) - (S(x-1)*S(x)+S(x)*S(x+1))+ (S(x-1)*(-S(x))+(-S(x))*S(x+1));
end
end
end
3 index in E(3,1) may not exactly be an integer. In your case, index row index 3 is generated by multiplying t*5 i.e. 0.6*5 (if t=0.6). It does not guarantee it to be an integer.
In a high accuracy check on generated index value 3, you will find that it is off from the exact integer 3 by 1 bit or so at its least significant end.
Therefore, while indexing E(3,1), 3 is not perceived as an integer.
In cases where you generate the index by multiplying with a decimal, make sure to convert it to int before using it for indexing such as round(t*0.5) or int8(t*0.5).
Or all together avoid index which are generated by multiplying the decimals.

Convert a decimal number that is not integer to base 4 in Matlab?

Is there a way to convert a decimal number between $0$ and $1$ that is not integer to base 4 in Matlab? E.g. if I put 2/5 I want to get 0.12121212... (with some approximation I guess)
The function dec2base only works for integers.
Listed in this post is a vectorized approach that works through all possible combinations of digits to select the best one for the final output as a string. Please note that because of its very nature of creating all possible combinations, it would be memory intensive and slower than a recursive approach, but I guess it could be used just for fun or educational purposes!
Here's the function implementation -
function s = dec2base_float(d,b,nde)
%DEC2BASE_FLOAT Convert floating point numbers to base B string.
% DEC2BASE_FLOAT(D,B) returns the representation of D as a string in
% base B. D must be a floating point array between 0 and 1.
%
% DEC2BASE_FLOAT(D,B,N) produces a representation with at least N decimal digits.
%
% Examples
% dec2base_float(2/5,4,4) returns '0.1212'
% dec2base_float(2/5,3,6) returns '0.101211'
%// Get "base power-ed scaled" digits
scale = b.^(-1:-1:-nde);
%// Calculate all possible combinations
P = dec2base(0:b^nde-1,b,nde)-'0';
%// Get the best possible combination ID. Index into P with it and thus get
%// based converted number with it
[~,idx] = min(abs(P*scale(:) - d));
s = ['0.',num2str(P(idx,:),'%0.f')];
return;
Sample runs -
>> dec2base_float(2/5,4,4)
ans =
0.1212
>> dec2base_float(2/5,4,6)
ans =
0.121212
>> dec2base_float(2/5,3,6)
ans =
0.101211

Using matlab,how to find the last two digits of a decimal number?

How can one find the last two digits of a decimal number using MATLAB?
Example:
59 for 1.23000659
35 for 56368.35
12 for 548695412
There will always be issues when you have a decimal number with many integer digits and fractional digits. In this case, the number of integer and decimal digits decide if we are correct or not in estimating the last two digits. Let's take at the code and the comments thereafter.
Code
%// num is the input decimal number
t1 = num2str(num,'%1.15e') %// Convert number to exponential notation
t1 = t1(1:strfind(t1,'e')-1)
lastind = find(t1-'0',1,'last')
out = str2num(t1(lastind-1:lastind)) %// desired output
Results and Conclusions
For num = 1.23000659, it prints the output as 59, which is correct thanks
to the fact that the number of integer and decimal digits don't add upto
more than 16.
For num = 56368.35, we get output as 35, which is correct again and the
reason is the same as before.
For num = 548695412, we are getting the correct output of 12 because of the
same good reason.
For an out of the question sample of num = 2736232.3927327329236576
(deliberately chosen a number with many integer and fractional digits),
the code run gives output as 33 which is wrong and the reason could be
inferred from the fact that integer and decimal digits add upto a much
bigger number than the code could handle.
One can look into MATLAB command vpa for getting more precision, if extreme cases like the 4th one are to dealt with.
Convert to string and then extract the last two characters:
x = 1.23; % x = 1.23
s = num2str(x); % s = "1.23"
t = s(end-1:end); % t = "23"
u = str2num(t); % u = 23
Note: depending on your specific needs you might want to supply a precision or formatSpec to num2str.
The other two answers are nice and straight forward, but here you have a mathematical way of doing it ;)
Assuming a as your number,
ashift=0.01*a; %shift the last two digits
afloor=floor(ashift); %crop the last two digits
LastDecimals=a-100*afloor; %substract the cropped number form the original, only the last two digits left.
Of course if you have non-natural numbers, you can figure those out too with the same "floor and subtract technique as above.

Hamming weight for a list of integers in Matlab [duplicate]

This question already has answers here:
Calculating Hamming weight efficiently in matlab
(9 answers)
Closed 9 years ago.
Quite a simple problem: I have a list of integers, e.g.,
a = [7 8]
Now I want to have a seperate list, that contains the Hamming Weight (that is the
number of 1 bits in the binary represenation) for each of the integers in the list. That means the result for the integer list above should look as follows:
res = [3 1]
Anyone an idea how I could do this quickly?
This is a little hacky, but it works:
res = sum( dec2bin(a).' == '1' );
It converts a to binary representation, looks at how many characters in that representation are '1', and sums up those numbers.
#% Quickly for a few or quickly for millions?
#% A quick method for a 32 bit int requires a 16 bit look-up table
#% Ideally the table is created once and passed to the function for usage
#% vectorized
vt=randi(2^32,[4096*4096,1])-1; #% input vector vt
num_ones=uint8(zeros(65536,1));
for i=0:65535
num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ;
end % 0.43 sec to create table
v=num_ones(mod(vt,65536)+1)+num_ones(floor(vt/65536)+1); #% 0.85 sec
% dec2bin is 1000 times slower

Filter some type of data in float

how to filter out float values with only zeroes beyond decimal & others having some non-zero values beyond decimal too.
for example.
13.000000
13.120001
i want it like this:
13.0
13.120001
If your application stores the floating point values in something like C/C++'s float's or double's, you may have a problem here.
First of all, float/double in the majority of cases represents values using base-2 and when you do something like double x = 0.1;, x in fact will never be equal to 0.1, but it will be very close to 0.1. That is because not every decimal (base-10) fraction can be represented exactly in base-2.
When you print or convert that x to a string using one of the printf-like functions, the resultant string can vary from something like 0.099999999 to 0.1 to 0.10000000000000000555. The result will depend on what's in x and on how you convert it to a string (how many digits you allow for the integer part, fractional part or all).
Generally there's no one-size-fits-all solution here. In one case the exactness and rounding issues can be unimportant, while in another they can be the most important. When performance is more important than the exact representation of decimal fractions, you use base-2 types. Otherwise, for example, when you're counting money, you use special types (and you may first need to construct them) that can represent decimal fractions exactly and then you avoid some of the described issues at a cost of extra computation time and maybe storage.
You may sometimes use fixed-point types and arithmetic. For example, if your numbers should not have more than 3 fractional digits and aren't too big, you can use scaled integers:
long x = 123456; // x represents 123.456
You add, subtract and compare them just as regular integers, but you multiply and divide them differently, for example like this:
long x = 123456; // x represents 123.456
long y = 12345; // y represents 12.345
long p = (long)(((long long)x * y) / 1000); // p=1524064, representing x*y=1524.064
long q = (long)((long long)x * 1000) / y); // q=10000, representing x/y=10.000
And in this case it is relatively easy to figure out the digits of the fractional part, just look at (x%1000)/100, (x%100)/10 and x%10.
split 13.00000 or 13.20001 into 2 values like in 13.0000 as 13 and 0000 ...so compare 2nd value whether it is greater than 0 (ex:13.20001 where 2001>0) if its yes then set same value . else (ex 13.0000 where 0000=0) then set %.2f=> 13.00