假设你有一个一维矩阵
a = rand(1,5);
[sa i] = sort(a);那么sa和a(i)是一样的。但是,如果矩阵的大小增加
a = rand(3,4);
[sa i] = sort(a);那么sa和a(i)就不一样了。当我试图按三维矩阵排序时,情况也是一样的。
如何通过索引a访问i的值?或者换句话说,我如何计算sa=a(X),X应该是什么?
编辑:
谢谢你的解决方案。但是,当您将维度更改为按其排序时,它们就无法工作。尽管如此,我还是采纳了这个想法,并使用它构建了一个通用的表单。
该算法所做的就是建立矩阵的索引。MATLAB索引单元格列为wise。因此,该索引由
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维。谢谢你的帮助,你照亮了道路。:)
发布于 2011-04-13 04:03:03
[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发布于 2011-04-13 15:14:43
通过使用IND2SUB和SUB2IND函数,您可以创建一个通用的向量化解决方案,它将适用于任何N矩阵或排序维度。在这里,我将这个解决方案打包到一个新的函数sort_linear_index中,它的行为将与函数排序一样,只不过它将返回线性索引,这样不管A大小如何,B = A(IX)总是工作的。
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发布于 2011-04-13 04:15:35
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)https://stackoverflow.com/questions/5643614
复制相似问题