How to assign an element of a column array to a variable?
2 views (last 30 days)
Show older comments
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);
5 Comments
Adam
on 18 Sep 2014
You are assigning new random data to each of them. If you want the same data just define the others in terms of the first one you assign.
You can setup random seed I think to ensure each call to rand starts from the same seed and would thus give the same sequence for a given length, but for your case that seems overboard and the above method would be far easier.
Accepted Answer
David Young
on 18 Sep 2014
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!