Vectorizing a recursive for-loop
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Noah Chrein
il 4 Ago 2015
Commentato: Noah Chrein
il 4 Ago 2015
Hey, I need help vectorizing this for-loop. In general, I need help vectorizing a for-loop with a term that references it's previous value.
In my case, DT is already a vector, and I want to fill out the Time vector as such
Time = zeros(1,N)
Time(1) = DT(1)/2
for i=2:N
Time(i)=Time(i-1)+0.5*(DT(i)+DT(i-1));
end
Is there a simple vectorization to this for loop?
Thank you.
0 Commenti
Risposta accettata
Sean de Wolski
il 4 Ago 2015
Modificato: Sean de Wolski
il 4 Ago 2015
Usually cumsum and diff will be the two functions that help referencing adjacency.
Time2 = cumsum([DT(1)/2 0.5*(DT(2:end)+DT(1:end-1))]);
And to check
isequal(Time,Time2)
ans = 1
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!