MATLAB排序2D和3D矩阵并通过索引访问

5

假设您有一个1D矩阵

a = rand(1,5);
[sa i] = sort(a);

那么saa(i)是相同的。然而,如果矩阵的大小增加了

a = rand(3,4);
[sa i] = sort(a);

然后saa(i)不相同。当我尝试通过其第三维对3D矩阵进行排序时,也会发生同样的情况。
我如何通过索引i访问a的值?换句话说,我该如何计算sa=a(X)X应该是什么?
编辑:
感谢各位提供的解决方案。然而,当您更改要排序的维度时,它们无法工作。尽管如此,我学到了思路,并使用它来构建一个通用形式。
算法所做的是构建矩阵的索引。 MATLAB按列索引单元格。因此,索引由下式给出:
idx = r + (c-1)*ROWS + (p-1)*ROWS*COLS

在这里,idx代表索引,r代表行位置,c代表列位置,p代表页位置。

因此,如果我们按第一维(普通的sort(a))排序,结果索引就是在列中的位置;如果我们按第二维排序,结果索引就是在行中的位置;如果我们按第三维排序,结果索引就是在页中的位置。话虽如此,还需要为给定情况生成行和列:

r = repmat((1:rows)',[1 cols pages]);
c = repmat(1:cols,[rows 1 pages]);

在给出的解决方案中已经解释了第一维度的排序。现在,让我们来对一个二维数组按照行的方式进行第二维度(即列)排序。

a = rand(4,5);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
[sa idx] = sort(a,2);
nIdx = R + (idx-1)*rows;
isequal(sa,a(nIdx))

现在,如果我们想在第三个维度(页面)上使用相同的排序思路,我们需要执行以下操作:

a = rand(4,5,3);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
C = repmat(1:cols,[rows 1 pages]);
[sa idx] = sort(a,3);
nIdx = R + (C-1)*rows + (idx-1)*rows*cols;
isequal(sa,a(nIdx))

同样的逻辑可以用于将其扩展到N维。感谢您的帮助,您照亮了道路。:)


2D 和 3D 的最佳答案就在这里。 - Trefex
4个回答

4

[sa, i]=sort(a) 返回每列的有序索引。您只需要获取矩阵的正确线性索引。因此,对于一个二维矩阵,

A=rand(3,4);
[rows,cols]=size(A);
[B,index]=sort(A,1);
correctedIndex=index+repmat(0:cols-1,rows,1)*rows;

现在进行测试:
A =

    0.9572    0.1419    0.7922    0.0357
    0.4854    0.4218    0.9595    0.8491
    0.8003    0.9157    0.6557    0.9340

B =

    0.4854    0.1419    0.6557    0.0357
    0.8003    0.4218    0.7922    0.8491
    0.9572    0.9157    0.9595    0.9340

A(correctedIndex)

ans =

    0.4854    0.1419    0.6557    0.0357
    0.8003    0.4218    0.7922    0.8491
    0.9572    0.9157    0.9595    0.9340

此外,您还可以查看 sortrows 函数,其第二个输出参数可用于实现类似于 sa=a(i,:) 的操作,使用您的符号表示法。 - user616736
谢谢,你让我朝着正确的方向前进。我明白如何使用索引。我扩展了你的解决方案,使其可以在任何维度上进行排序。谢谢! - adn

2
您可以创建一个通用的向量化解决方案,适用于任何N维矩阵或排序维度,使用函数IND2SUBSUB2IND。在这里,我将这个解决方案打包成一个新函数sort_linear_index,它的行为就像函数SORT一样,只是它将返回线性索引,以便B = A(IX)始终能够正常工作,无论A的大小如何。
function [sortedA,sortIndex] = sort_linear_index(A,sortDim,sortOrder)
%#SORT_LINEAR_INDEX   Just like SORT, but returns linear indices

  sizeA = size(A);  %# Get the matrix size
  if nargin < 2
    sortDim = find(sizeA > 1,1);  %# Define sortDim, if necessary
  end
  if nargin < 3
    sortOrder = 'ascend';  %# Define sortOrder, if necessary
  end
  [sortedA,sortIndex] = sort(A,sortDim,sortOrder);  %# Sort the matrix
  [subIndex{1:numel(sizeA)}] = ...  %# Create a set of matrix subscripts
     ind2sub(sizeA,reshape(1:prod(sizeA),sizeA));
  subIndex{sortDim} = sortIndex;  %# Overwrite part of the subscripts with
                                  %#   the sort indices
  sortIndex = sub2ind(sizeA,subIndex{:});  %# Find the linear indices

end

现在我们可以测试一下这个函数:
>> A = rand(1,10);
>> [B,IX] = sort_linear_index(A);  %# Sort a row vector
>> isequal(B,A(IX))
ans =
     1
>> A = rand(3,4,3);
>> [B,IX] = sort_linear_index(A,1);  %# Sort a 3-by-4-by-3 matrix along
>> isequal(B,A(IX))                  %#   the first dimension
ans =
     1
>> [B,IX] = sort_linear_index(A,3);  %# Sort a 3-by-4-by-3 matrix along
>> isequal(B,A(IX))                  %#   the third dimension
ans =
     1
>> [B,IX] = sort_linear_index(A,2,'descend');  %# Sort a 3-by-4-by-3 matrix along
>> isequal(B,A(IX))                            %#   the second dimension
ans =                                          %#   in descending order
     1

1
a = rand(3,5);
[sa i] = sort(a);
ii=bsxfun(@plus,i,0:size(a,1):numel(a)-size(a,1));
isequal(a(ii),sa)

0

是的,我使用了循环操作。然而,我在想是否有一些使用矩阵操作或索引的技巧,因为它们更快。 - adn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接