MATLAB: Am I getting a “Subscript indices must either be real positive integers or logicals” error

concentrationsubscript indices

I have a script used to plot the buildup of a concentration N(i) over time. When a variable "ice" is one, I want it to use the first equation in my loop. When "ice" is 0, I want it to use the second equation. Whenever I run it, however, it gives me an error "Subscript indices must either be real positive integers or logicals." I…can't tell what that's referring to. My code is below. Can anybody help me? Thank you!
x = linspace(0,13000,1000);
ice = [1 1 1 1 1 1 1 1 1 1 1 0 0];
N = 0;
for i = 1:length(x)
if ice(i) == 1
N(i)= N(i-1)* .880606541;
else
N(i)= N(i-1)+ 46050.11434;
end
end
plot(x,N)

Best Answer

'Cuz you wrote
for i = 1:length(x)
if ice(i) == 1
N(i)= N(i-1)* .880606541;
and ice(1) is 1 so the RHS of the assignment statement for i=1 is
N(1)= N(0)*0.880606541;
which is zero subscript which as the error says is verboten; Matlab arrays are 1-based and immutable in that regard.
The typical solution is to go from 1:N+1 instead of 0:N
Related Question