如何避免循环以减少这段代码的计算时间?

How to avoid the loop to reduce the computation time of this code?

如何避免循环以减少此代码的计算时间(one solution of my last question):

我希望找到 A(1:3,:) 的列向量,其在 M(4,:) 中的对应值不属于单元格 X 的向量之一(并且显然不等于一个这些载体)。如果 X 非常大,我会寻找快速解决方案。

M = [1007  1007  4044  1007  4044  1007  5002 5002 5002 622 622;
      552   552   300   552   300   552   431  431  431 124 124; 
     2010  2010  1113  2010  1113  2010  1100 1100 1100  88  88;
        7    12    25    15    12    30     2   10   55  32  12];

这里我直接拿A:

A = [1007  4044  5002  622;
      552   300   431  124;
     2010  1113  1100   88];

A 包含 M(1:3,:)

的唯一列向量
X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};

[~, ~, subs] = unique(M(1:3,:)','rows');

A4 = accumarray(subs(:),M(4,:).',[],@(x) {x});

%// getting a mask of which columns we want
idxC(length(A4)) = false;
for ii = 1:length(A4)
    idxC(ii) = ~any(cellfun(@(x) all(ismember(A4{ii},x)), X));
end

显示我们想要的列

out = A(:,idxC)

结果:

>> out

out =

    1007        4044
     552         300
    2010        1113

删除了列向量 [5002;431;1100],因为 [2;10;55] 包含在 X{2} = [2 10 55 9 17]

删除了列向量 [622;124;88] 因为 [32 12] = X{4}

另一个例子:X

    M = [1007  4044  1007  4044  1007  5002 5002 5002 622 622  1007  1007  1007;
          552   300   552   300   552   431  431  431 124 124   552    11    11; 
         2010  1113  2010  1113  2010  1100 1100 1100  88  88  2010    20    20;
           12    25    15    12    30     2   10   55  32  12     7    12     7];

X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};

A = [1007  4044  5002  622  1077;
      552   300   431  124    11;
     2010  1113  1100   88    20];

结果:(带有 scmg 答案)

我得到 if A 根据第一行排序:(正确结果)

out =

         1007        1007        4044
           11         552         300
           20        2010        1113

如果我不对矩阵 A 进行排序,我会得到:(错误结果)

out =

        4044        5002         622
         300         431         124
        1113        1100          88

应该删除列向量 A(:,4) = [622;124;88],因为 [32 12] = X{4}

应该删除列向量 [5002;431;1100],因为 [2;10;55] 包含在 X{2} = [2 10 55 9 17]

也许你可以使用2次cellfun:

idxC = cellfun(@(a) ~any(cellfun(@(x) all(ismember(a,x)), X)), A4, 'un', 0);
idxC = cell2mat(idxC);
out = A(:,idxC)

在这种情况下,您不应该试图消除循环。矢量化实际上正在严重伤害你。

特别是(为您的匿名 lambda 命名)

issubset = @(x) all(ismember(A4{ii},x))

效率低得离谱,因为它不会短路。将其替换为循环。

也一样
any(cellfun(issubset, X))

改用与此类似的方法:

idxC = true(size(A4));
NX = numel(X);
for ii = 1:length(A4)
    for jj = 1:NX
        xj = X{jj};
        issubset = true;
        for A4i=A4{ii}
            if ~ismember(A4i, xj)
                issubset = false;
                break;
            end;
        end;
        if issubset
            idxC(ii) = false;
            break;
        end;
    end;
end;

两个 break 语句,尤其是第二个语句,会触发提前退出,这可能会为您节省大量计算。

Ben Voigt 的回答很好,但是 for A4i = A4{ii} 行是导致问题的原因:for 循环不适用于列向量:

%row vector
for i = 1:3
    disp('foo');
end

    foo
    foo
    foo

%column vector
for i = (1:3).'
    disp('foo');
end

    foo

只需尝试 A4i = A4{ii}.' 即可完成您的工作!

现在,如果我们查看输出:

A(:,idxC) =

    4044        5002
     300         431
    1113        1100

如您所见,最终的结果并不是我们所期望的。

只要unique做一种排序,subs就不是按照在A中遇到的顺序编号,而是按照在C中遇到的顺序编号(已排序):

subs =

 2
 2
 3
 2
 3
 2
 4
 4
 4
 1
 1

因此,您应该通过 unique 而不是 A 给出的矩阵来获得最终输出

输入

[C, ~, subs] = unique(M(1:3,:)','rows'); 
%% rather than [~, ~, subs] = unique(M(1:3,:)','rows');

然后,要获得最终输出,请输入

>> out = C(idxC,:).'
out =

        1007        4044
         552         300
        2010        1113

镜头#1

本节中列出的方法应该是解决我们案例的一种快速 直接 方法。请注意,由于 A 是从 M 考虑到第三行的唯一列的矩阵,因此在此处将其作为输入跳过,因为我们使用解决方案代码在内部生成它。这也在下一个 approach/shot 中得到维护。这是实现 -

function out = shot1_func(M,X)

%// Get unique columns and corresponding subscripts
[unqrows, ~, subs_idx] = unique(M(1:3,:)','rows');
unqcols = unqrows.'; %//'

counts = accumarray(subs_idx(:),1); %// Counts of each unique subs_idx

%// Modify each cell of X based on their relevance with the fourth row of M
X1 = cellfun(@(x) subs_idx(ismember(M(4,:),x)),X,'Uni',0);

lensX = cellfun('length',X1); %// Cell element count of X1

Xn = vertcat(X1{:}); %// Numeric array version of X
N = max(subs_idx);   %// Number of unique subs_idx

%// Finally, get decision mask to select the correst columns from unqcols
sums = cumsum(bsxfun(@eq,Xn,1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);

mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).';    %//'
out = unqcols(:,~decision_mask);

return

镜头#2

前面提到的方法可能有瓶颈:

cellfun(@(x) subs_idx(ismember(M4,x)),X,'Uni',0)

因此,或者为了保持表现作为一种良好的动力,可以将整个过程分为两个阶段。第一阶段可以处理 X 的单元格,这些单元格在 M 的第四行中没有重复,这可以通过矢量化方法实现,另一个阶段解决 X's 的其余部分使用我们较慢的 cellfun 方法的细胞。

因此,代码会有点膨胀,但希望性能会更好。最终的实现看起来像这样 -

%// Get unique columns and corresponding subscripts
[unqrows, ~, subs_idx] = unique(M(1:3,:)','rows')
unqcols = unqrows.' %//'
counts = accumarray(subs_idx,1);

%// Form ID array for X
lX = cellfun('length',X)
X_id = zeros(1,sum(lX))
X_id([1 cumsum(lX(1:end-1)) + 1]) = 1
X_id = cumsum(X_id)

Xr = cellfun(@(x) x(:).',X,'Uni',0); %//'# Convert to cells of row vectors
X1 = [Xr{:}]                         %// Get numeric array version

%// Detect cells that are to be processed by part1 (vectorized code)
[valid,idx1] = ismember(M(4,:),X1)
p1v = ~ismember(1:max(X_id),unique(X_id(accumarray(idx1(valid).',1)>1))) %//'

X_part1 = Xr(p1v)
X_part2 = Xr(~p1v)

%// Get decision masks from first and second passes and thus the final output
N = size(unqcols,2);
dm1 = first_pass(X_part1,M(4,:),subs_idx,counts,N)
dm2 = second_pass(X_part2,M(4,:),subs_idx,counts)
out = unqcols(:,~dm1 & ~dm2)

相关函数-

function decision_mask = first_pass(X,M4,subs_idx,counts,N)

lensX = cellfun('length',X)'; %//'# Get X cells lengths
X1 = [X{:}];                  %// Extract cell data from X

%// Finally, get the decision mask
vals = changem(X1,subs_idx,M4) .* ismember(X1,M4);

sums = cumsum(bsxfun(@eq,vals(:),1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);
mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).';    %//'
return


function decision_mask = second_pass(X,M4,subs_idx,counts)

%// Modify each cell of X based on their relevance with the fourth row of M
X1 = cellfun(@(x) subs_idx(ismember(M4,x)),X,'Uni',0);

lensX = cellfun('length',X1); %// Cell element count of X1

Xn = vertcat(X1{:}); %// Numeric array version of X
N = max(subs_idx);   %// Number of unique subs_idx

%// Finally, get decision mask to select the correst columns from unqcols
sums = cumsum(bsxfun(@eq,Xn,1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);

mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).';       %//'

return

验证

本节列出了验证输出的代码。这是验证镜头 #1 代码的代码 -

%// Setup inputs and output
load('matrice_data.mat');   %// Load input data
X = cellfun(@(x) unique(x).',X,'Uni',0); %// Consider X's unique elements
out = shot1_func(M,X); %// output with Shot#1 function

%// Accumulate fourth row data from M based on the uniqueness from first 3 rows
[unqrows, ~, subs] = unique(M(1:3,:)','rows');    %//'
unqcols = unqrows.';                              %//'
M4 = accumarray(subs(:),M(4,:).',[],@(x) {x});    %//'
M4 = cellfun(@(x) unique(x),M4,'Uni',0);

%// Find out cells in M4 that correspond to unique columns unqcols
[unqcols_idx,~] = find(pdist2(unqcols.',out.')==0);

%// Finally, verify output
for ii = 1:numel(unqcols_idx)
    for jj = 1:numel(X)
        if all(ismember(M4{unqcols_idx(ii)},X{jj}))
            error('Error: Wrong output!')
        end
    end
end
disp('Success!')