Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

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

Asked by Brooke

Brooke (view profile)

on 27 Dec 2011
Accepted Answer by bym

bym (view profile)

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 Comments

Jan Simon

Jan Simon (view profile)

on 27 Dec 2011

Please post the relevant part of the code instead of describing it.

Brooke

Brooke (view profile)

on 27 Dec 2011

@Jan: I have posted the code.

Brooke

Brooke (view profile)

Products

3 Answers

Answer by bym

bym (view profile)

on 27 Dec 2011
Accepted answer

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 Comments

Brooke

Brooke (view profile)

on 27 Dec 2011

@Walter and @proecsm:

I'm still getting

EDU>> testing
??? Subscript indices must either be real positive integers or
logicals.

Error in ==> testing at 7
y(n)=log(abs(x(n)));
 
7   y(n)=log(abs(x(n)));

With that code! Why o why? :((

bym

bym (view profile)

on 27 Dec 2011

do a clear; before running your script

Brooke

Brooke (view profile)

on 27 Dec 2011

@proecsm: Thanks, that worked!! :)

bym

bym (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 27 Dec 2011

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 Comments

Walter Roberson

Walter Roberson (view profile)

on 27 Dec 2011

Does that mean that if you comment out the assignment to y(n) that your code will start? If that is the case then you would have gotten a very different error than the one you describe: it takes serious syntax errors to keep the code from running at all.

Please make things faster for yourself by posting the relevant code so that we are not left guessing about what is going on.

Brooke

Brooke (view profile)

on 27 Dec 2011

@Walter: Thanks, I have posted the code.

Thanks, That works

Jan Simon

Jan Simon (view profile)

Answer by Sean de Wolski

Sean de Wolski (view profile)

on 27 Dec 2011
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 Comment

Brooke

Brooke (view profile)

on 27 Dec 2011

@Sean: Sorry, that was a typo, I have corrected it to 2 but the problem still persists.

Sean de Wolski

Sean de Wolski (view profile)

Contact us