Performance 快速比较矩阵行的方法?

Performance 快速比较矩阵行的方法?,performance,matlab,matrix,vectorization,Performance,Matlab,Matrix,Vectorization,我有一个代码,在运行过程中重复几次。此代码将C的行逐行进行比较。它需要很长时间才能运行,特别是对于大型C。我正在寻找一种快速的方法来运行它。我使用了a(ismember(a,b))而不是intersect(a,b) 这将是一种方法- %// Get the size of C and pairwise indices N = size(C,1); [Y,X] = find(bsxfun(@gt,[1:N]',[1:N])); %//' %// Form the two pairs p1 = C

我有一个代码,在运行过程中重复几次。此代码将
C
的行逐行进行比较。它需要很长时间才能运行,特别是对于大型
C
。我正在寻找一种快速的方法来运行它。我使用了
a(ismember(a,b))
而不是
intersect(a,b)

这将是一种方法-

%// Get the size of C and pairwise indices
N = size(C,1);
[Y,X] = find(bsxfun(@gt,[1:N]',[1:N])); %//'

%// Form the two pairs
p1 = C(X,:);
p2 = C(Y,:);

%// Get the matches for each element in a row against all other elements in
%// the matching row pair combinations. This would be equivalent to
%// ISMEMBER implementation in the original code
matches = squeeze(any(bsxfun(@eq,permute(p1,[3 2 1]),permute(p2,[2 3 1])),1));
matches = reshape(matches,[],numel(Y)); %//for special case when C has 2 rows

%// Get linear indices that satisfy the equality criteria from original code
idx = find(sum(matches.'.*p1,2) == sum(p2,2)); %//'

%// Store the row and column information from the linear indices
K_vectorized = Y(idx);
K1_vectorized = X(idx);

C
的典型数据大小是什么?谢谢您的提示。
C
的行数最多可达1500行。它有更少的列(最多5列)。你想用它做什么?@knedlsepp:我想找到多余的行。冗余行是指具有其他行元素子集和零的行。例如,如果
C=[17 18 5;3 5 7;17 18 0;4 17 2;5 0 0]
,那么
C(3,:)
C(5,:)
是多余的。因此,
n=2
K=[35]
K1n=[12]
@Meher81:您的原始代码与您在注释中描述的不完全一样。只有当
C
的条目不是负数时,此项才有效。您的代码对某些C无效。我找不到原因。例如,请测试它的
C=[3,4,0,0;4,8,0,0;]
@Divakar:
C
包含零和正整数。@Meher81必须添加一行来处理该特殊情况。现在就去看看。
%// Get the size of C and pairwise indices
N = size(C,1);
[Y,X] = find(bsxfun(@gt,[1:N]',[1:N])); %//'

%// Form the two pairs
p1 = C(X,:);
p2 = C(Y,:);

%// Get the matches for each element in a row against all other elements in
%// the matching row pair combinations. This would be equivalent to
%// ISMEMBER implementation in the original code
matches = squeeze(any(bsxfun(@eq,permute(p1,[3 2 1]),permute(p2,[2 3 1])),1));
matches = reshape(matches,[],numel(Y)); %//for special case when C has 2 rows

%// Get linear indices that satisfy the equality criteria from original code
idx = find(sum(matches.'.*p1,2) == sum(p2,2)); %//'

%// Store the row and column information from the linear indices
K_vectorized = Y(idx);
K1_vectorized = X(idx);