首页 > 解决方案 > “parfor 中的变量无法分类”MATLAB

问题描述

我正在尝试将我的代码转换为使用 parfor 运行,因为它本身需要很长时间才能运行。但是我不断收到此错误。我在网站上四处搜索并阅读过有类似问题的人,但这些答案似乎都没有解决我的问题。这是我的代码:

r = 5;

Mu = 12.57e-9;

Nu = 12e6;

I = 1.8;

const = pi*Nu*Mu*r*I;

a = 55;

b = 69;

c = 206;

[m,n,p] = size(Lesion_Visible);

A = zeros(m,n,p);

parpool(2)

syms k

parfor J = 1:m
     for  I = 1:n
         for K = 1:p
             if Lesion_Visible(J,I,K) ~= 0
                  Theta = atand((J-b)/(I-a));
                  Rho = abs((I-a)/cosd(Theta))*0.05;
                  Z = abs(c-K)*0.05;
                  E = vpa(const*int(abs(besselj(0,Rho*k)*exp(-Z*k)*besselj(0,r*k)),0,20),5);
                  A (J,I,K) = E;
             end
         end
    end
end

我正在尝试计算阵列上特定位置的电场,matlab 给我错误“parfor 中的变量 A 无法分类”。我需要帮助。谢谢。

标签: matlabfor-loopparallel-processingclassificationparfor

解决方案


由于不允许在 parfor 循环中对变量进行分类,因此您应该尝试将每个循环的输出保存在一个变量中,然后将最终输出保存到所需的变量中,在您的情况下为A!这应该做的工作 -

parfor J = 1:m

B=zeros(n,p); %create a padding matrix of two dimension 
for  I = 1:n
    C=zeros(p); %create a padding matrix of one dimension
       for K = 1:p
         if Lesion_Visible(J,I,K) ~= 0
              Theta = atand((J-b)./(I-a));
              Rho = abs((I-a)./cosd(Theta))*0.05;
              Z = abs(c-K).*0.05;
              E = vpa(const.*int(abs(besselj(0,Rho.*k).*exp(-Z.*k).*besselj(0,r.*k)),0,20),5);
              C(K) = E; %save output of innnermost loop to the padded matrix C
         end
       end
     B(I,:)=C; % save the output to dim1 I of matrix B


end
A(J,:,:)=B; save the output to dim1 J of final matrix A
end

通过以下内容更好地理解 - http://www.mathworks.com/help/distcomp/classification-of-variables-in-parfor-loops.html http://in.mathworks.com/help/distcomp/sliced -variable.html


推荐阅读