2.28 Sort each column (on its own) in a matrix

Given

     4     2     5 
     2     7     9 
    10     1     2
 

Sort each column on its own, so that the result is

     2     1     2 
     4     2     5 
    10     7     9
 

In Matlab, the sort command is used. But in the Mathematica, the Sort command is the same the Matlab’s sortrows() command, hence it can’t be used as is. Map is used with Sort to accomplish this.

Mathematica

mat={{4,2,5}, 
     {2,7,9}, 
     {10,1,2}}; 
 
Map[Sort[#]&,Transpose[mat]]; 
mat=Transpose[%]
 

{{2, 1, 2}, 
 {4, 2, 5}, 
 {10,7, 9}}
 

 

Matlab

A=[4  2 5; 
   2  7 9; 
   10 1 2]; 
sort(A,1)
 

     2     1     2 
     4     2     5 
    10     7     9
 

 

Maple

restart; 
restart; 
A:=Matrix([[4,2,5],[2,7,9],[10,1,2]]); 
map(x->sort(x),[LinearAlgebra:-Column(A,[1..-1])]); 
Matrix(%); 
 
#or, may simpler is 
 
restart; 
A:=Matrix([[4,2,5],[2,7,9],[10,1,2]]); 
for n from 1 to LinearAlgebra:-ColumnDimension(A)  do 
    A[..,n]:=sort(A[..,n]); 
od: 
A
 

Matrix(3, 3, [[2, 1, 2], 
              [4, 2, 5], 
              [10, 7, 9]])