フィルターのクリア

Why is Matlab not abled to run this as a parfor loop?

1 回表示 (過去 30 日間)
Matthias
Matthias 2013 年 4 月 19 日
I want to run a loop like this as a parfor loop. In my eyes it shouldn't matter in which order the different iterations are called. 'a' is only used as reduction variable.
a = zeros(1,5);
parfor i= 1:1000
dts = randi(10)-1;
if dts == 0
a(1) = a(1) + 2;
elseif dts <= 5
a(dts) = a(dts) +1;
end
end
This is the error, which i get:
Error using Test (line 2) Error: The variable a in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview". Why wouldn't Matlab allow this program? How do i fix it?
Edit: Since the Codeblock-function doesn't seem to work: http://pastebin.com/FUHFe61T
  2 件のコメント
Sean de Wolski
Sean de Wolski 2013 年 4 月 19 日
That doesn't even work with a regular for-loop. That should always be the first step: making sure it works with a regular loop.
Matthias
Matthias 2013 年 4 月 19 日
I had a little typo. It is supposed to be randi(10) not rand(10).

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

採用された回答

Friedrich
Friedrich 2013 年 4 月 19 日
編集済み: Friedrich 2013 年 4 月 19 日
Hi,
MATLAB thinks a is a sliced variable and you get troubles with the indices here. I think in such a case its better to use a Reduction Variable like this:
X = zeros(1,5);
parfor i= 1:1000
a = zeros(1,5);
dts = randi(10)-1;
if dts == 0
a(1) = (a(1) + 2);
elseif dts <= 5
a(dts) = a(dts) +1;
end
X = X + a;
end
For more information about variable types see here:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by