1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
function [C,P] = argsort (A, arg1, arg2)
% GRB.ARGSORT sort the rows or columns of a matrix
%
% [C,P] = argsort (A)
% [C,P] = argsort (A, 'ascend')
% [C,P] = argsort (A, 'descend')
% [C,P] = argsort (A, dim)
% [C,P] = argsort (A, dim, 'ascend')
% [C,P] = argsort (A, dim, 'descend')
%
% GrB.argsort sorts the rows or columns of A. By default, the
% columns of A are sorted (dim == 1); with dim = 2, the rows of A are
% sorted. The default is to sort in ascending order.
%
% Example:
%
% A = sprand (20,10,0.5) ;
% [C,P] = GrB.argsort (A) ;
% A (:,1)
% C (:,1)
% P (:,1)
% % the MATLAB sort includes all implicit zeros:
% [B,I] = sort (A) ;
% B (:,1)
% I (:,1)
%
% This methods differs from the MATLAB sort function. Implicit zeros
% are ignored and always placed last in the output. P is returned sparse,
% and only reflects the entries in A, not the implicit zeros. The MATLAB
% [C,P] = sort (A) always returns P as full since it permutes the implicit
% zeros of A as well. Complex matrices are not supported.
%
% See also sort, GrB.argmin, GrB.argmax.
% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0
if (isobject (A))
A = A.opaque ;
end
dim = 1 ;
direction = 'ascend' ;
if (nargin == 3)
dim = arg1 ;
direction = arg2 ;
elseif (nargin == 2)
if (ischar (arg1))
direction = arg1 ;
else
dim = arg1 ;
end
end
if (nargout == 1)
C = gbargsort (A, dim, direction) ;
else
[C,P] = gbargsort (A, dim, direction) ;
P = GrB (P) ;
end
C = GrB (C) ;
|