Parallelize cumulative average algorithm
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Radek
le 6 Mar 2024
Réponse apportée : Edric Ellis
le 6 Mar 2024
Hi,
is there a way how to parallelize algorithm computing average of data accessible only as a stream using parfor?
dat = 1:9;
avg = 0;
for i=1:numel(dat)
avg = (dat(i)+(i-1)*avg)/i;
end
avg
In reality the code looks more like the following, where the input data is too big to be stored.
avg = 0;
for i=1:numFrames
frm = getNewFrame(); % returns new data
avg = (frm+(i-1)*avg)/i;
end
I somehow understand why this code can not be parallelized as is, but maybe Matlab offers some trick.
0 commentaires
Réponse acceptée
Edric Ellis
le 6 Mar 2024
You can use a parfor "reduction variable" to compute this, with a slight modification which may or may not be significant:
dat = rand(1,9);
avg = 0;
N = numel(dat);
parfor i = 1:N
avg = avg + dat(i)/N;
end
avg, mean(dat)
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!