필터 지우기
필터 지우기

??? Subscript indices must either be real positive integers or logicals

조회 수: 3 (최근 30일)
Brooke
Brooke 2011년 12월 27일
I have a for loop that gives the values of x(n) -- here n is an index.
d=0.5;
 
x(1)=1;
 
for n=2:10
x(n)= x(n-1)+d;
y(n)= log(abs(x(n)));
fprintf('y(%d)=%d', n, y(n))
end
I want the loop to also give the values of y(n) = log(abs(x(n))).
However, MATLAB doesn't seem to like this definition and I am presented with the error message: ??? Subscript indices must either be real positive integers or logicals.
What should I do to fix it?
Thanks.
  댓글 수: 2
Jan
Jan 2011년 12월 27일
Please post the relevant part of the code instead of describing it.
Brooke
Brooke 2011년 12월 27일
@Jan: I have posted the code.

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

채택된 답변

bym
bym 2011년 12월 27일
matlab is ones based, not zeros... x(0) is an error
[edit]
works fine for me (added \n in fprintf statement)
d=0.5;
x(1)=1;
for n=2:10
x(n)= x(n-1)+d;
y(n)= log(abs(x(n)));
fprintf('y(%d)=%d\n', n, y(n))
end
y(2)=4.054651e-001
y(3)=6.931472e-001
y(4)=9.162907e-001
y(5)=1.098612e+000
y(6)=1.252763e+000
y(7)=1.386294e+000
y(8)=1.504077e+000
y(9)=1.609438e+000
y(10)=1.704748e+000
  댓글 수: 4
bym
bym 2011년 12월 27일
do a clear; before running your script
Brooke
Brooke 2011년 12월 27일
@proecsm: Thanks, that worked!! :)

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

추가 답변 (2개)

Jan
Jan 2011년 12월 27일
Either n is not an integer greater than 0 or the symbols log or abs have been defined as variables, such that the built-in functions are shadowed. You can test this by using:
dbstop if error
and start the program. Then Matlab stops when the error occurs and you can check the symbols by:
which abs
which log
The command whos can be helpful also.
  댓글 수: 6
Brooke
Brooke 2011년 12월 27일
@Walter: Thanks, I have posted the code.

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


Sean de Wolski
Sean de Wolski 2011년 12월 27일
x(n)= x(n-1)+d;
when n = 1, the first iteration of the loop, you're trying to reference x(1-1) a.k.a. x(0) which is undefined and the reason you're seeing the above error.
  댓글 수: 1
Brooke
Brooke 2011년 12월 27일
@Sean: Sorry, that was a typo, I have corrected it to 2 but the problem still persists.

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

카테고리

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