Matlab 2013a index error [duplicate] - matlab

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.

Related

Matlab Error"Array indices must be positive integers or logical values" [duplicate]

This question already has an answer here:
Subscript indices must either be real positive integers or logical error
(1 answer)
Closed 10 months ago.
I'm new to matlab and trying to express the function:
f(z)=(1-exp((1+v)./z)).*(z./y).*cos(z)
with the values: >> v=3.4 >> y=6.9 >> z=8:1.21:328.65
but always get the error message "Array indices must be positive integers or logical values."
Please help
If you are writing f(z) into matlab it means z index of array f. You need to say r = (1-exp((1+v)./z)).*(z./y).*cos(z):
v = 3.4;
y = 6.9;
z=8:1.21:328.65;
f = (1-exp((1+v)./z)).*(z./y).*cos(z);

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

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

Subscript indices must either be real positive integers or logicals.? [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 6 years ago.
I encounter this message, " Subscript indices must either be real positive integers or logicals."
Error in ==> plot(b(s,1),b(s,2),'r*').
this is my code
ptsIntersect=floor(ptsIntersect);
for s1=1:numBorderPoints
d1=sqrt((b(s1,1)-ptsIntersect(1,1)).^2 + (b(s1,2)-ptsIntersect(1,2)).^2);
if (d1<2)
break;
end
end
plot(b(s1,1),b(s1,2),'*')
% find second point of border in intersection
for s2=1:numBorderPoints
d2=sqrt((b(s2,1)-ptsIntersect(2,1)).^2 + (b(s2,2)-ptsIntersect(2,2)).^2);
if (d2<2)
break;
end
end
plot(b(s2,1),b(s2,2),'*')
if ( s2-s1>5)
s=(s1+s2)/2;
hold on
plot(b(s,1),b(s,2),'r*')
end
Just before the error, both s1 and s2 are positive integers. But when you do s=(s1+s2)/2, it might happen that s is not an integer, e.g. (3+2)/2=2.5.
Type s in the command window and its value will be displayed.