How to avoid that loop
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alberto Menichetti
il 3 Mag 2016
Commentato: Steven Lord
il 3 Mag 2016
k(1) = 1;
for i=2:size(k)
k(i) = k(i-1)*v(i)
end
v(i) is a scalar and it's different on every iteration How could I do that without using a loop?
2 Commenti
the cyclist
il 3 Mag 2016
Modificato: the cyclist
il 3 Mag 2016
As written, this loop will never be executed, because size(k) is 1, and
for i = 2:1
<stuff>
end
will have zero iterations.
Some coding mistake? Maybe you meant length(v)?
Risposta accettata
the cyclist
il 3 Mag 2016
If my speculation about what you meant it correct, then
k = cumprod(v)/v(1)
2 Commenti
Steven Lord
il 3 Mag 2016
No, we understand you. Another approach that doesn't involve division:
v = randperm(8) % Sample data for demonstration purposes
k = cumprod([1 v(2:end)])
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!