使用ismember将嵌套的for循环转换为将两个数组的列匹配为parfor循环

时间:2018-10-08 21:11:32

标签: matlab parfor

我有以下嵌套的for循环,该循环使用ismember来标识两个数组具有相同值的位置。我这样做是为了找到与较小数组tp相关的数据(见下文)。

% xw is a 1xM array of position data
% yw is a 1xN ...
% zw is a 1xP ...
TP = combvec(xw',yw',zw')';    % you must have the deep learning toolbox to use this for the latest version of Matlab 2018b
in = inpolyhedron(f,v,TP);     % I would have done inpolyhedron(f,v,Xw,Yw,Zw) where Xw, Yw, and Zw are meshgrids from xw, yw, and zw but doing so exceeds the maximum array size in MatLab.
tp = TP(in,:);  Ltp = length(tp);
in3 = zeros(N,M,P);

for l=1:Ltp
   for ii=1:M
      for jj=1:N
         for kk=1:P
             if ismember(tp(l,:),TP,'rows')
                in3(jj,ii,kk) = ismember(tp(l,:),TP,'rows');
             end
         end
      end
   end
end

a = A(in3);     % A is a NxMxP array
% a should also be a NxMxP array with the majority of the points being zero, and the points corresponding to tp should be non-zero.

我尝试应用here所描述的内容,但是由于使用ismember并填充3D数组,因此我不确定如何使其起作用。

使用for循环可以很好地运行,但是我想使过程更快,我认为parfor循环是最好的选择。

0 个答案:

没有答案