Array Indice Error For Loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
HoboHarry
am 25 Okt. 2018
Bearbeitet: Stephen23
am 25 Okt. 2018
Following code is coming up with error "Array indices must be positive integers or logical values."
Does someone know what is causing this. Trying to store each value into an array
z =@(x) tan(x);
h = 0.5
for x=1:0.5:10
Output (x) = (z(x+h)-z(x-h))/(2*h);
end
Array = [Output]
1 Kommentar
Akzeptierte Antwort
madhan ravi
am 25 Okt. 2018
Bearbeitet: madhan ravi
am 25 Okt. 2018
z =@(x) tan(x);
h = 0.5
x=1:0.5:10
Output = zeros(1,19) %preallocation for speed and efficiency
for i = 1:numel(x)
Output (i) = (z(x(i)+h)-z(x(i)-h))/(2*h);
end
Array = Output
3 Kommentare
Stephen23
am 25 Okt. 2018
@madhan ravi: where is the array preallocation? What do those square brackets do? (Hint: nothing)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!