Wrong indexing in parfor loop

I tried to use parfor to change parts of a variable but receive unexpected result
d = ones(10,10);
d = repmat(d,[1 1 1 10]);
d(:,:,2,:) = .5*d(:,:,1,:);
parfor n=1:10;
d(:,:,2,n) = 2*d(:,:,2,n);
end
I expected to receive something like
d(:,:,2,:) = d(:,:,1,:);
Instead I get
d(:,:,1,:) = 2;
d(:,:,2,:) = .5;
What went wrong? Using for instead of parfor works fine.
Thanks

5 件のコメント

Joachim Schlosser
Joachim Schlosser 2015 年 11 月 10 日
Can you please verify your results? I correctly get all matrices with ones.
Walter Roberson
Walter Roberson 2015 年 11 月 10 日
Also which MATLAB version is being used on which operating system?
Andre Zeug
Andre Zeug 2015 年 11 月 10 日
編集済み: Andre Zeug 2015 年 11 月 10 日
Thanks for support.
I'm using R2014a and Win7-64bit. Get same (unexpected) result after system restart.
Adam Barber
Adam Barber 2015 年 11 月 10 日
I was able to reproduce this on R2014a, but not R2015b on my system.
Andre Zeug
Andre Zeug 2015 年 11 月 10 日
編集済み: Andre Zeug 2015 年 11 月 10 日
I could now reproduce this unexpected result also on R2013b.

サインインしてコメントする。

回答 (1 件)

Andre Zeug
Andre Zeug 2015 年 11 月 11 日
編集済み: Andre Zeug 2015 年 11 月 11 日

0 投票

Thanks to the community. Summarizing, the problem of wrong indexing in PARFOR loops appears in R2014a (and older versions?) but is not an issue in R2015a and later.
Here a simple workaround for Matlab users with releases before introduction of HG2:
d = ones(10,10,10,10);
% slice variable
dSub = d(:,:,2,:);
% if memory is an issue
d(:,:,2,:) = [];
parfor n=1:10;
% do manipulation to subset only
dSub(:,:,:,n) = 2*dSub(:,:,:,n);
end
% if memory is an issue
d(:,:,[1 3:10],:) = d;
% include the manipulated part
d(:,:,2,:) = dSub;
clear dSub

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2015 年 11 月 10 日

編集済み:

2015 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by