代码之家  ›  专栏  ›  技术社区  ›  masher

Matlab中的parfor:parfor中的变量不能分类

  •  0
  • masher  · 技术社区  · 6 年前

    我有以下几点 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
    

    我做错什么了?有没有可能把这个平行化?

    1 回复  |  直到 6 年前
        1
  •  0
  •   oro777    6 年前

    我认为这个错误是由于 parfor documentation