我要执行以下parfor
循环。
我遇到了错误:
Error: File: Coords_to_distances4.m Line: 32 Column: 9
The variable nr in a parfor cannot be classified.
指向内部for循环的最后一行。
nr
是在循环外部声明和初始化的,我看不到变量索引之间的任何其他链接,或者其他所有解决方案都在谈论。
coords = ... %nx2 array of x and y coordinates
n = length(coords); %n can be up to several million
dr = 0.01; %distance resolution for upcoming calculations
nr = [zeros(200000,1)]; %array to hold counts of distances up to maximum of 200000*dr
parfor i = 1:n
for j = i+1:n
dx = coords(i,1) - coords(j,1); %get separation in x
dy = coords(i,2) - coords(j,2); % and in y
r = sqrt(dx*dx + dy*dy); %calculates Pythagorean distance
nbin = floor(r/dr + 0.5); %rounds to the nearest integer
nr(nbin) = nr(nbin) + 1; %uses the distance as an index to the array to incremenet
end
end
我在做什么错?可以并行化吗?
答案 0 :(得分:0)
我认为该错误是由于parfor
的局限性引起的
在documentation中,“要进行正确的变量分类,必须通过常数或变量来定义嵌套在parfor循环中的for循环的范围”