Why do I get "Array indices must be positive integers or logical values"?

delta_x = 2/40;
x = [];
for i = 1:1:41
x = x (i-1) * delta_x;
end

1 Comment

This is one of the most helpfully descriptive error messages you get in Matlab. It tells you exactly what the problem is!

Sign in to comment.

 Accepted Answer

Yes because index has to be positive where your loop starts from 1 if you index x as i-1 it means it’s zero that’s why the error arises. An example:
delta_x = 2/40;
x = [1:41];
for i = 1:length(x)
x(i) = x(i)*delta_x;
end
x

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!