フィルターのクリア

Parallelize cumulative average algorithm

1 回表示 (過去 30 日間)
Radek
Radek 2024 年 3 月 6 日
回答済み: Edric Ellis 2024 年 3 月 6 日
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.

採用された回答

Edric Ellis
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
Starting parallel pool (parpool) using the 'Processes' profile ... Parallel pool using the 'Processes' profile is shutting down.
avg, mean(dat)
avg = 0.6135
ans = 0.6135

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by