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
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
2018년 10월 21일
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
2018년 10월 22일
Walter Roberson
2018년 10월 21일
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
2018년 10월 22일
Harry Brown
2018년 10월 22일
Walter Roberson
2018년 10월 22일
You missed the 1: in the for loop
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!