Array indices must be positive integers or logical values.

I am trying to ploy a graph of speed against time. But I keep getting this error.
hold on
for t = 0:0.1:25
if t < 5
speed(t) = 0.1553567.*(t.^6) - 2.0416.*(t.^5) + 9.1837.*(t.^4) - 14.829.*(t.^3) - 1.3703.*(t.^2) + 32.821.*t - 1.3155;
elseif t >= 5 && t < 15.4
speed(t) = 0.003980879.*(t.^5) - 0.2247.*(t.^4) + 4.8682.*(t.^3) - 50.442.*(t.^2) + 254.67.*t - 430.66;
else
speed(t) = -0.073.*(t.^2) + 6.1802.*t + 40.423;
end
end
plot(t,speed(t),'-')
hold off

回答 (2 件)

Image Analyst
Image Analyst 2018 年 10 月 21 日

1 投票

Your t is not an index - an integer - and it needs to be. The FAQ has a thorough discussion: https://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F

3 件のコメント

Harry Brown
Harry Brown 2018 年 10 月 21 日
So how would I get it to range from 0 to 25?
Image Analyst
Image Analyst 2018 年 10 月 22 日
tVector = 0:0.1:25
for k = 1 : length(t)
t = tVector(k);
speed(k) = same as before, using t (not tVector).....
Use a loop iterator k which is an integer. Then compute all the t before in a vector and extract the one single t you need for that particular iteration inside the loop and use it.
Harry Brown
Harry Brown 2018 年 10 月 22 日
Ah I see, cheers.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2018 年 10 月 21 日

1 投票

Learn to use this design pattern:
vals = ... appropriate vector
num_vals = length(vals);
output = zeros(1, num_vals);
for idx = 1 : num_vals
this_val = vals(idx);
... calculation involving this_val goes here ...
output(idx) = ... appropriate value
end
...
plot(vals, output)
Alternately, use
for T = 1:1:251
t = (T-1)/10;
if t < 5
speed(T) = 0.1553567.*(t.^6) - 2.0416.*(t.^5) + 9.1837.*(t.^4) - 14.829.*(t.^3) - 1.3703.*(t.^2) + 32.821.*t - 1.3155;
elseif t >= 5 && t < 15.4
speed(T) = 0.003980879.*(t.^5) - 0.2247.*(t.^4) + 4.8682.*(t.^3) - 50.442.*(t.^2) + 254.67.*t - 430.66;
else
speed(T) = -0.073.*(t.^2) + 6.1802.*t + 40.423;
end
end
plot((T-1)/10, speed)
or
for t = 0:0.1:25
T = round(t*10) + 1;
if t < 5
speed(T) = 0.1553567.*(t.^6) - 2.0416.*(t.^5) + 9.1837.*(t.^4) - 14.829.*(t.^3) - 1.3703.*(t.^2) + 32.821.*t - 1.3155;
elseif t >= 5 && t < 15.4
speed(T) = 0.003980879.*(t.^5) - 0.2247.*(t.^4) + 4.8682.*(t.^3) - 50.442.*(t.^2) + 254.67.*t - 430.66;
else
speed(T) = -0.073.*(t.^2) + 6.1802.*t + 40.423;
end
end
plot(0:0.1:25, speed)
The first design pattern can be used even when the values are irregularly spaced or when it is difficult to calculate an index given a value. The control value associated with any one location is always the same because the control values are pre-calculated and recalled as needed.
The second one, using integer indices and calculating values from there, can produce more precise control values than you might get from using a floating point increment such as for 0:0.1:25 . For example, the 4th entry of 0:0.1:25 is not exactly the same as you would get from coding 0.3 but the 4th entry of (0:250)/10 is exactly the same as you would get from coding 0.3 .
The third one, using floating point increment for the 0:0.1:25 and calculating indices from it, is likely to be the least accurate, and can get you in trouble with floating point comparisons, but might be the most convenient to code... until, that is, you encounter the subtle problems with floating point accuracy, at which point it becomes a nuisance.

3 件のコメント

Harry Brown
Harry Brown 2018 年 10 月 22 日
Okay, thanks for the help
Harry Brown
Harry Brown 2018 年 10 月 22 日
I have edited my code to this. But when I try to plot the values, I get zero for speed up until the final time, where it jumps up to the correct value.
hold on
Vector_t = 0:0.1:25;
for k = length(Vector_t)
t = Vector_t(k);
if t < 5
speed(k) = 0.1553567.*(t.^6) - 2.0416.*(t.^5) + 9.1837.*(t.^4) - 14.829.*(t.^3) - 1.3703.*(t.^2) + 32.821.*t - 1.3155;
elseif t >= 5 && t < 15.4
speed(k) = 0.003980879.*(t.^5) - 0.2247.*(t.^4) + 4.8682.*(t.^3) - 50.442.*(t.^2) + 254.67.*t - 430.66;
else
speed(k) = -0.073.*(t.^2) + 6.1802.*t + 40.423;
end
output(k) = speed(k);
end
plot(Vector_t,output,'*')
hold off
Walter Roberson
Walter Roberson 2018 年 10 月 22 日
You missed the 1: in the for loop

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2018 年 10 月 21 日

再開済み:

2018 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by