How to assign an element of a column array to a variable?

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

I copied your code and ran it and I don't get the error - is what you're running exactly what you've shown here?
Looks fine to me though is that exactly the code that gave the error? It is pointing to a line3, but the code it shows as causing the problem is not the 3rd line of that code.
Sri
Sri 2014년 9월 18일
편집: Sri 2014년 9월 18일
Nn=input('Number of samples: ');
M=2+2*rand(Nn,1);
NN=2+2*rand(Nn,1);
RR=0.35+0.25*rand(Nn,1);
d0=rand(Nn,1);
Ns=input('Specified Number of cycles: ');
count=0;
d=zeros(Nn,1);
ni=zeros(Nn,1);
for i=1:Nn
[D(i),Ni(i)]=initiation_1( d0,N,N0,Nc,R1,RR,M,NN )
end
function [ D,Ni ] = initiation_1 (d0,N,N0,Nc,R1,RR,M,NN)
loading=input('Is the loading amplitude constant?(Y/N)[Enter / to termiante]: ','s');
if (loading=='Y')||(loading=='y')
[D,Ni]=constant_loading_1(d0,N0,Nc,R1,RR,NN,M,N);
d(i,1)=D;
ni(i,1)=Ni;
function [ D,Ni ] = constant_loading_1(d0,N0,Nc,R1,RR,NN,M,N)
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));
Ni=ceil(N0+((Nc/(n+1)).*(power(1-D0,n+1)).*(power(R1/(R1-r),m))));
end
The values in the column vectors d0,M,NN,RR and D0,m,n,r are different. Why is this happening?
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

David Young
David Young 2014년 9월 18일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

질문:

Sri
2014년 9월 18일

댓글:

2014년 9월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by