Octave / Matlab:确定非常大的矩阵中的唯一行

Octave / Matlab : Determine unique rows in very large matrix

我在 Octave 中有一个 13146 x 13146 的矩阵,我想确定它的唯一行。 unique(M, "rows") 由于 Octave 内部 and/or 内存限制而失败。

我查看了其他关于查找唯一行的帖子,但其中 none 解决了大型矩阵的这个问题。

我现在的方法是 "divide and conquer",例如。 G。通过

A(:,:,i)=M(r_i:s_i,:)
B(:,:,i)=unique(A(:,:,i), "rows")

i子矩阵的索引,r_is_i子矩阵的开始和结束行号。 要return将所有数据放入一个大矩阵(并再次确定唯一行):

C(:,:,i)=B(:,:,i)'
D=reshape(C,l,n*m)
E=D'
F=unique(E, "rows")

其中 n 子矩阵的数量,m 子矩阵中的原始行数和 l 列的数量。

有没有更好的方法达到预期效果?

基数排序吧!

听起来您需要一种内存高效的排序算法。可以通过首先对行进行排序,然后检查相邻行的重复项来找到唯一行。您可以为此调整基数排序,按顺序对每一列进行排序(而不是按顺序对每个数字进行排序)。这将是对一列而不是整个矩阵进行排序的峰值内存成本。然后遍历排序结果中的行并消除重复项。这是一个 O(n) 操作,只需要足够的内存来容纳两行。

也可以是"stable"。如果在排序过程中除了跟踪重新排列的行值之外还跟踪重新排列的行索引,则可以计算输入-输出映射索引。 (这些是 Matlab 自己的 [B,I] = sort(A) 签名中的 I。)这反过来又允许您将 post-duplicate-removal 行重新排列回输入中的原始顺序,这样您就可以保留它们的顺序。 (类似于 Matlab 的 unique()setOrder='stable' 选项。)它们还可以用于计算整体唯一性操作的输入输出映射索引,因此您可以重现 [= 的完整多输出签名16=],这很有用。

示例代码

这是一个基本的示例实现。我还没有对它进行彻底的测试,所以不要在没有自己测试的情况下在生产中使用它。

function A = rrunique(A)
%RRUNIQUE "Radix Row Unique" - find unique rows using radix sort
%
% # Returns the distinct rows in A. Uses the memory-efficient radix sort
% # algorithm, so peak memory usage stays low(ish) for large matrices.

% # This uses a modified radix sort where only the row remapping indexes are
% # rearranged at each step, instead of sorting the whole input, to avoid
% # having to rewrite the large input matrix.

ix = 1:size(A,1); % # Final in-out mapping indexes

% # Radix sort the rows
for iCol = size(A,2):-1:1
    c = A(ix,iCol);
    [~,ixStep] = sort(c);
    % # Don't do this! too slow
    % # A = A(ixStep,:);
    % # Just reorder the mapping indexes
    ix = ix(ixStep);
end    

% # Now, reorder the big array all at once
A = A(ix,:);

% # Remove duplicates
tfKeep = true(size(A,1),1);
priorRow = A(1,:);
for iRow = 2:size(A,1)
    thisRow = A(iRow,:);
    if isequal(thisRow, priorRow)
        tfKeep(iRow) = false;
    else
        priorRow = thisRow;
    end
end
A = A(tfKeep,:);

end

当我在 OS X 上的 Matlab R2014b 上对您大小的矩阵进行测试时,它的峰值使用了大约 3 GB 的内存,而仅容纳输入矩阵的内存约为 1 GB。还不错。

>> m = rand([13146,13146]);
>> tic; rrunique(m); toc
Elapsed time is 17.435783 seconds.

这里的基本思路和一样,只是后期实现有点不同。因此,我们对输入数组的行进行排序,使重复项彼此重叠。然后,我们 运行 通过行查找重复项,这可以使用 diff 高效地完成。从 diff 输出中,我们检测到 all zeros 行,它们代表那些重复的行。我们从 detection 中创建一个逻辑掩码,并使用此掩码从输入数组中提取 valid 行。这是实现,这似乎使用了 unique(...'rows') -

一半的内存
sM = sortrows(M);
out = sM([true ; any(diff(sM,[],1)~=0,2)],:);

您可以在列上使用滑动 window 并使用不会导致内存问题的 window 大小。这是一个解决方案

function A = winuninque(A, winSz)
nCol = size(A,2);
I = zeros(size(A,1), 0);
for k=1:winSz:nCol
    [~, ~, I] = unique([I A(:,k:min(k+winSz-1,end))], 'rows');
end
[~, I] = unique(I);
A = A(I, :);
end

基准

为了实际有一些重复的行,最好生成一些重复的矩阵,否则它只是排序。以下是不同方法之间的比较:

>> A=repmat(rand(13146,13146), 2, 1);
>> A=A(randperm(end), :);
>> A=A(1:end/2,:);

>> tic; B=rrunique(A); toc
Elapsed time is 13.318752 seconds.
>> tic; C=winunique(A, 16); toc
Elapsed time is 6.606122 seconds.
>> tic; D=unique(A, 'rows'); toc
Elapsed time is 29.815333 seconds.
>> isequal(B,C,D)
ans =
     1
>> size(D)
ans =
        9880       13146