필터 지우기
필터 지우기

Why can't Matlab calculate a(0)

조회 수: 2 (최근 30일)
Michael Bernard
Michael Bernard 2013년 2월 4일
Hi everyone, its not serious problem at all. But I just want to know. Why cant Matlab calculate a(0) in a for loop.
Example: n=3;L=4;
>> ai=[2 4 6 8];
for m=0:n
an(m)=-ai(L-m)/ai(1)
end
Matlab will give an error message that says
"Attempted to access (0); index must be a positive integer or
logical."
My question is why?? I get results if I use
for m=1:n
an(m)=-ai(L-m)/ai(1)
end
because a(1) , a(2) etc can be found in matlab. so why not a(0)???

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 4일
편집: Azzi Abdelmalek 2013년 2월 4일
It's just how Matlab works, allows only positive integer or logical index. You can resolve the problem, for example
for k=0:10
y(k+1)=sin(k)
end
  댓글 수: 1
Michael Bernard
Michael Bernard 2013년 2월 4일
Thanks a lot Azzi Abdelmalek .Its well understood

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 2월 4일
You need to distinguish between array notation and function notation.
Array notation requires that the arguments describe integer array locations. Array locations in MATLAB are numbered starting from 1, as is the case for Fortran and a number of other computer languages (but not all -- e.g., C numbers starting from 0.)
function notation is what is used when you say things like
f(1.37) = 8.2
or
an(0) = 4
That notation is completely valid in mathematics, but it has no equivalent in MATLAB, except inside MuPAD (the symbolic toolbox).

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by