Seems like a bug in Matlab R2015a "sort" routine

조회 수: 1 (최근 30일)
Nikolay S.
Nikolay S. 2016년 3월 22일
댓글: Guillaume 2016년 3월 23일
a =[24 9 27 14; 1 40 9 33; 17 16 31 35];
[aSort, iSort] = sort(a, 2, 'descend');
% Those should be exactly the same
isequal(a(iSort), aSort)
But they are not!
Lets see what do we have:
aSort =
27 24 14 9
40 33 9 1
35 31 17 16
iSort =
3 1 4 2
2 4 3 1
4 3 1 2
Apparently iSort is totally messed up! It is not global indexing as it should be!!! It should span 1:numel(a), but instead is spans 1:size(a, 2)- only indexes along sorted dimension! Seems like a severe bug to me. One has to play with "ind2sub" and "sub2ind" to get it right...

채택된 답변

Guillaume
Guillaume 2016년 3월 22일
The bug is in your interpretation of the indices returned by sort. As per the documentation of sort in R2015a:
[B,I] = sort(...) additionally returns a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a numeric vector, B = A(I).
It behaves exactly as documented. This may not be what you want but it is what it does. It's not particularly difficult to transform the column indices into linear indices:
iSort = sub2ind(size(A), repmat((1:size(A, 1))', 1, size(A, 2)), iSort);
  댓글 수: 3
Guillaume
Guillaume 2016년 3월 23일
Nikolay S. comment moved from Answer to here:
Guillaume thanks for the solution. For the given case it works (though one may find the code a bit complicated). However, what it would be for N dimensional matrix, sorted along dimension K? I have failed to generate code for this general case.
Guillaume
Guillaume 2016년 3월 23일
Yes, a generic code is a lot more complicated. This would work:
A = randi([1 100], [10 20 15 5]); %demo data
k = 3; %demo data
[B, isort] = sort(A, k);
%build subscript indices arrays with ndgrid:
subs = cell(1, ndims(A)); %store all subscript arrays in a cell array
[subs{:}] = size(A); %size of each dimension
subs = cellfun(@(s) 1:s, subs, 'UniformOutput', false); %convert to vectors of 1:size(dim)
[subs{:}] = ndgrid(subs{:}); %convert to arrays of subscripts
subs{k} = isort; %replace subscript for kth dimension by isort
isort = sub2ind(size(A), subs{:});
isequal(A(isort), B) %should return 1

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by