Array indices must be positive integers or logical values error

조회 수: 6 (최근 30일)
Hope Damogbe
Hope Damogbe 2019년 3월 16일
편집: Hope Damogbe 2019년 3월 16일
I am trying to perform the following on MATLABIssue on matlab.PNG
However, I keep getting the error which says "Array indices must be positive integers or logical values error"
Please help. Here's my code:
nT=0
for n=0:10000 %part 5
beta=alpha*sqrt(1-((2*n)/N-1)^2);
if abs(n)<= (N-1)/2
syms k x
Ibeta=1+symsum((((1/factorial(k))*(beta/2)^k)^2),k,1,Inf)
Ialpha=1+symsum((((1/factorial(k))*(alpha/2)^k)^2),k,1,Inf)
wk(nT)=Ibeta/Ialpha
nT=nT+1;
else
wk(nT)=0
nT=nT+1;
end
n=n+1;
end
THIS IS THE ERROR I GET:
Array indices must be positive integers or logical values.
Error in DSPproj (line 25)
wk(nT)=Ibeta/Ialpha

채택된 답변

Walter Roberson
Walter Roberson 2019년 3월 16일
You initialize nT to 0. You access wk(nT) but nT is still 0.
You need to move the increments of nT to before you use nT as indices.
  댓글 수: 2
Hope Damogbe
Hope Damogbe 2019년 3월 16일
편집: Hope Damogbe 2019년 3월 16일
Ohh, doesnt initializing nT to 0 mean that in the first loop interation, I am setting a value for wk(0) in the wk array?
Because that's what I thought. Correct me if I'm wrong.
**Edit:
Actually I just realized to access the first element of the array I need to use index 1 ( wk(1) ). I was using C++ logic. Thank you for your help, it works now!
Walter Roberson
Walter Roberson 2019년 3월 16일
Yes, but array indices start at 1 in MATLAB. 0 is not a valid index in MATLAB.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by