The variable x in a parfor cannot be classified
2 ビュー (過去 30 日間)
古いコメントを表示
When I run this code, I get an error "The variable x in a parfor cannot be classified" that points to x(j,:) in the line sum = sum + A(i,j)*x(j,:);. Can you please suggest what should I do?
parfor i = 2:n
while (j < i)
if (A(i,j) ~= 0)
sum = sum + A(i,j)*x(j,:);
end
j = j+1;
end
end
1 件のコメント
KSSV
2019 年 1 月 10 日
YOu need not to use a parfor for this. YOu can achieve it stright away by vectorization which would be very fast.
回答 (1 件)
Edric Ellis
2019 年 1 月 10 日
Hm, I didn't see that precise error. I modified your example just a little so that it was actually executable, like so:
n = 4;
A = rand(n);
x = rand(n);
sum = 0; % note 1
parfor i = 2:n
j = 1; % note 2
while (j < i)
if (A(i,j) ~= 0)
sum = sum + A(i,j)*x(j,:);
end
j = j+1;
end
end
A couple of changes:
- I set an initial value for sum
- You must reset j each time around the parfor loop so that the parfor machinery can tell that you intend j to be a temporary variable (and that you aren't doing anything order-dependent).
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!