Matlab Number (Figure) Representation [duplicate] - matlab

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am confused with how numbers are represented in Matlab.
Considering this sequence of computations, carried out using 64-bit arithmetic in
Matlab:
format longg
i = 0:8;
a = sin(i*(pi/2))
When doing these calculations by hand, I would expect to see the results a = 0, 1, 0, –1, 0,
etc. But that is not what Matlab shows. Why not? Is it because pi is not in machine number?
In addition,
format long
a = 2^(60);
b = 20000;
c = a + b;
d = c – a
When doing these calculations by hand, I would expect to see the result d = 20000. But that is not what Matlab returns. Why not?

Related

check if two matrix are equivalent [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 3 years ago.
I got a problem about checking if two matrices are equal in MATLAB.
Specifically, I want to verify if, for a matrix W, all elements are equal to each other and all row sums are equal to 1 we would have W = W^2.
Therefore I wrote the following code which aims to check if every element of these two matrices are equivalent, to conclude whether the matrices are equal to each other. But it turns out that this does not work since the W8 matrix should be equal to its square.
for i = 1 :60
for j = 1 :60
if(W8(i,j) - W8_square(i,j) ~= 0)
disp('the matrix are not equal');
break;
end
end
end
There is a matlab function for it:
eq = isequal(W8,W8_square) should work
Here you find the reference
https://www.mathworks.com/help/matlab/ref/isequal.html
Be careful that if this checks for EXACT identity, so computation errors, of the order of eps, may affect the result.
To solve this, I would subtract the two matrixes and check the norm of the result: below a certain threshold (low) they are equal.
Here you have an example code for your problem:
n = 10; %matrix size
W8 = ones(n)/n; %Generating W8
W8_square = W8^2;
eq = isequal(W8,W8_square) %checking EXACT identity
M_difference = W8-W8_square; %Difference matrix
eq2 = isequal(M_difference<=eps,ones(n)) %%comparing every value with eps

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.

Value does not change for any obvious reason - Matlab [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 7 years ago.
I wrote the following code. The problem is that at the specific value of 0.288,
T turns to zero for no obvious reason. Any explanation? The weird thing is that when I change it to 0.28 it works fine.
time=(0:0.002:0.560);
time_f=reshape(time,281,1);
time1=0;
time2=0;
for i=1:1
for j=1:281
T=time_f(j,i);
if (i==1) && (T==0.288);
time1=T;
end
end
end
If you test the code you will find that the time1 value will be zero and if you change T to 0.28 it will work.
The answer is simple
>> time_f(time_f == 0.288)
ans =
Empty matrix: 0-by-1
Your matrix doesn't contain the value 0.288
This is due to float precision, so instead of being 0.288, your value is 0.287999999 for example.
Just use roundn, which round to a specified number of significant digits
>> time_f = roundn(time_f,-3);
>> find(time_f == 0.288)
ans =
145
If you don't have Mapping toolbox you can do
>> time_f = round(time_f * 1000) / 1000
You are using floating point arithmetic, which has certain precision errors. Your matrix does not contain exactly the value 0.288.

Compare displayed digits of double in Matlab [duplicate]

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

reflecting random walk in matlab? [duplicate]

This question already has answers here:
Matlab -- random walk with boundaries, vectorized
(2 answers)
Closed 8 years ago.
I have a array of 10 vectors 'x' with as below (for simulating 1D random walk):
r=rand(10,1000);
r(r>.5)=1;
r(r<=.5)=-1;
x=cumsum(r);
The image of the one vector will be like:
If I consider 2 values in the sequence , say +10 and -10, then I would like to reflect the sequence 'x' when it reaches those values. How to achieve this?
Before answering your question, a should point that your code is broken. By default cumsum accumulate data across first dimension, to change this behavior you should specify dim parameter:
x = cumsum(r,2);
And, answering your question, you could simply invert all data above threshold:
threshold = 10;
nsteps = ceil( max(abs(x(:))) / (2*threshold) - 0.5 );
for ii = 1:nsteps
ind = abs(x) > 10;
x(ind) = 20 * sign(x(ind)) - x(ind);
end