Parallelize cumulative average algorithm
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Edric Ellis
2024 年 3 月 6 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!