Question: Sort Matrix elements in ascending or descending order

December 12 2012 herclau 993
Maple
0

How to successfully deploy Matlab function [B, IX] = sort (A, ...)
IX is a permutation vector of the corresponding column of A

M := Matrix(6, 3, [2, 3, 3, 5, 7, 8, 12, 5, 9, -3, 4.1, 7, 7, 7, -3, 9, 3, 8]):
M;
L := <(seq(op(convert(sort(M[() .. (), j]), list)), j = 1 .. op(M)[2]))>;
L := ArrayTools[Alias](L, [op(M)[1 .. 2]], Fortran_order);
P := Matrix(op(M)[1 .. 2],  (i, j)-> ListTools[Search](L[i, j], M[() .. (), j]));
# from Matlab
# [b,n]=sort(A)

# b =

#    -3.0000    3.0000   -3.0000
#     2.0000    3.0000    3.0000
#     5.0000    4.1000    7.0000
#     7.0000    5.0000    8.0000
#     9.0000    7.0000    8.0000
#    12.0000    7.0000    9.0000


# n =

#      4     1     5
#      1     6     1
#      2     4     4
#      5     3     2
#      6     2     6
#      3     5     3
#
#

Gracias

Please Wait...