How to store loop results where loop does not have whole numbers only
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Stef
am 25 Jun. 2018
Bearbeitet: Stephen23
am 25 Jun. 2018
I want to store the loop results, however my loop is not only whole numbers. In the code you can see what I want to do, but it does not work, because n is not a whole number.
y = 10
A = zeros(1,10)
for n = 4:0.5:6
A(n) = n * y
end
This does not work since e.g A(4.5) is not possible because there only positive subscripts are obviously allowed.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 25 Jun. 2018
Bearbeitet: Stephen23
am 25 Jun. 2018
The easiest and most robust solution is to move those values into their own variable, defined before the loop. Then you can simply make the loop iterator the required indices:
y = 10
V = 4:0.5:6;
A = zeros(1,numel(V))
for k = 1:numel(V)
A(k) = V(k) * y
end
0 Kommentare
Weitere Antworten (1)
Torsten
am 25 Jun. 2018
y = 10;
A = zeros(1,5);
iCount = 0;
for n = 4:0.5:6
iCount = iCount + 1;
A(iCount) = n * y
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown 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!