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

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.

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);

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.

Error about Subscript indices of sum function for float matrix matlab [duplicate]

This question already has an answer here:
matlab strange behaviour of function sum()
(1 answer)
Closed 7 years ago.
I have a matrix 202x141x3 that called M matrix. I want to perform the code
sum(M,3);
However, I got error such as
Subscript indices must either be real positive integers or logicals.
I debugged and saw the content in M matrix, it has not problem. But I cannot use the above function. Could you see my M.mat matrix at here and let me know how can I use above function for my M.mat matrix
You did set a variable under the name sum
a = [1 2 3 ; 4 -5 6; 7 8 9]
sum = 1;
>> sum(a)
Subscript indices must either be real positive integers or logicals.
clear sum;
>> sum(a)
ans =
12 5 18

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.

Subscript indices must be real positive integers, and they are (Matlab) [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 8 years ago.
I am trying to code a simple backpropagation network in Matlab, and I am getting the following error:
Subscript indices must either be real positive integers
or logicals.
in line 144 of my code, which is during this section:
for l = 1:net.layerCount,
if l == 1, % From input layer
for i = round(1:net.inputSize),
i % i = 1 and
l % l = 1 when I get the error.
% error in this next line:
net.weight{l}(i,:) = net.weight{l}(i,:) ...
- sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
- (momentum .* net.previousWeightDelta{l}(i,:));
net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
end
else
for i = 1:net.layerSize{l-1},
net.weight{l}(i,:) = net.weight{l}(i,:) ...
- sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
- (momentum .* net.previousWeightDelta{l}(i,:));
net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
end
end
end
The error persists even if I surround 1:net.layerCount and the other loop vectors with round(). Any idea why this is the case?
Thanks!
In the l == 1 case, you illegally try to use
net.layerOutput{l-1}
0 is not positive.
The input layer needs to use the inputs, not inter-layer connections.