What's wrong with this parfor loop?

When I try to run the following parfor loop, Matlab returns the error claiming that A cannot be classified.
N = 10;
T = 3;
A = zeros(N,T);
parfor i = 1:1:N
for t = 1:1:T
A(i,t) = rand(1,1);
end
end
Why isn't A considered to be a sliced output variable? The following modification works just fine.
N = 10;
T = 3;
A = zeros(N,T);
parfor i = 1:1:N
A(i,1) = rand(1,1);
end
So it seems to be something to do with the inner loop over t. From reading the help document on sliced variables, I don't understand why this inner loop would make a difference.

2 件のコメント

even coil
even coil 2015 年 12 月 11 日
Is there a workaround besides using the vectorized version of rand? (Doing the analgous would be difficult in the application I have in mind.)
Matt J
Matt J 2015 年 12 月 11 日
編集済み: Matt J 2015 年 12 月 11 日
If you interchange the order of the loops you've shown, it will run. Whether that's the best solution is hard to tell, though, without seeing an example closer to the application you have in mind.

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

 採用された回答

Edric Ellis
Edric Ellis 2015 年 12 月 11 日

1 投票

In this case, the fix is simply to remove the increment from the inner for loop, like so:
N = 10;
T = 3;
A = zeros(N,T);
parfor i = 1:1:N
for t = 1:T
A(i,t) = rand(1,1);
end
end
(I'm not sure why this limitation exists, it doesn't appear to be explicitly mentioned in the documentation)

3 件のコメント

Matt J
Matt J 2015 年 12 月 11 日
編集済み: Matt J 2015 年 12 月 11 日
(I'm not sure why this limitation exists, it doesn't appear to be explicitly mentioned in the documentation)
Maybe for the same reasons that this isn't supported:
parfor i = 1:1:N
for t = 1:i:T
A(i,t) = rand(1,1);
end
end
If A isn't pre-allocated, I can see parfor having a hard time figuring out what size(A) should ultimately be.
even coil
even coil 2015 年 12 月 11 日
Why isn't this considered a bug? Isn't "1:T" just shorthand for "1:1:T"?
Matt J
Matt J 2015 年 12 月 11 日
編集済み: Matt J 2015 年 12 月 11 日
Hard to say if it can be called a bug because it's not yet clear what level of support is intended for nested loops that interact with sliced variables. Note, for example, that the following also produces a classification error,
parfor i = 1:1:N
for t = 1:2:T
A(i,t) = rand(1,1);
end
end

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 12 月 11 日

1 投票

N = 10;
T = 3;
A = zeros(N,T);
parfor i = 1:1:N
v = zeros(1,T);
for t = 1:1:T
v(1,t) = rand(1,1);
end
A(i, :) = v;
end

1 件のコメント

even coil
even coil 2015 年 12 月 11 日
Thank you, this is a good workaround.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

質問済み:

2015 年 12 月 10 日

編集済み:

2015 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by