How to accumulate to a vector-indexed array?
Afficher commentaires plus anciens
I want to vectorize something in the following form:
for jk = 1:njk
k = K(jk);
L(k) = L(k) + R(jk);
end
where the range of the k values is less than that of the jk values, so some k values occur for multiple values of jk. The obvious "solution" would be
L(K(1:njk)) = L(K(1:njk)) + R(1:njk);
but this gives wrong results, either because (1) L(K(1:njk)) occurs on both the LH and RH sides, or because (2) vectored indexing cannot be used on the LH side (I'm not sure whether (1) or (2) is the reason). A possible approach might be if there is something equivalent to the "+=" operator in C, which would allow
L(K(1:njk)) += R(1:njk);
Also the vectors happen to be large (>10000 elements) so anything that involves creating an intermediate matrix ix not likely to be practical.
Is there a way to vectorize this?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!