MATLAB: How to assign an element of a column array to a variable

arrays

Sorry if this might seem silly, but I am not able to get this: I have :
d0=rand(Nn,1);
M=2+2*rand(Nn,1);
NN=2+2*rand(Nn,1);
RR=0.35+0.25*rand(Nn,1);
d=zeros(Nn,1);
ni=zeros(Nn,1);
for i=1:Nn
D0=d0(i,1);
r=RR(i,1);
n=NN(i,1);
m=M(i,1);
D=1-power(power(1-D0,n+1)-((N-N0)/Nc).*(n+1).*power((1-r/R1),m),1./(n+1));
d(i,1)=D;
end
gives:
Subscript indices must either be real positive integers or logicals.
Error in constant_loading_1 (line 3) D0=d0(i,1);

Best Answer

The variable i is not given a value in initiation_1. That means that it gets its default value, which is the square root of -1. Hence the error. A solution is to pass the value of i as an argument to the function.
In general, it is good practice to avoid using i and j as the names of ordinary variables, because of the possibility of obscure errors like this. You could use ii and jj instead, for example.