MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sorting a Matrix


  • To: mathgroup@smc.vnet.net
  • Subject: [mg12278] Re: sorting a Matrix
  • From: Paul Abbott <paul@physics.uwa.edu.au>
  • Date: Tue, 5 May 1998 03:30:15 -0400
  • Organization: University of Western Australia
  • References: <6ilpsl$jpo$12@dragonfly.wolfram.com>

Klamser wrote:

> with Sort[{a,f,e,s}] I can sort a Vector (a List). How can I sort a
> Matrix??
> 
> In the Help I can find the Information: Sort[{33,76,2,6,17},#1>#2&]
> 
> For a Matrix I think it should work like this:
> Sort[{33,76,2,6,17},#1[[#5,#3]]>#2[[#4,#3]]&]
> 
> where #3 is the column to be sorted for, and #5 and #4 are the different
> Vectors in the Matrix.
> 
> Can somebody help me??

For a random 4x4 matrix, say,

In[1]:= mat = Table[Random[], {4}, {4}] Out[1]= {{0.266234, 0.647371,
0.730429, 0.565464},  
  {0.00975624, 0.0234279, 0.571189, 0.196294},  
  {0.171129, 0.27151, 0.820526, 0.760194},  
  {0.201956, 0.988453, 0.666745, 0.569845}}

Sort, by default uses #1<#2&:

In[2]:= Sort[mat]
Out[2]= {{0.00975624, 0.0234279, 0.571189, 0.196294},  
  {0.171129, 0.27151, 0.820526, 0.760194},  
  {0.201956, 0.988453, 0.666745, 0.569845},  
  {0.266234, 0.647371, 0.730429, 0.565464}}

This has ordered the vectors by the first column.  To order by column 3,

In[3]:= Sort[mat, #1[[3]] < #2[[3]] & ] Out[3]= {{0.00975624, 0.0234279,
0.571189, 0.196294},  
  {0.201956, 0.988453, 0.666745, 0.569845},  
  {0.266234, 0.647371, 0.730429, 0.565464},  
  {0.171129, 0.27151, 0.820526, 0.760194}}

Cheers,
	Paul 

____________________________________________________________________ 
Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia            Nedlands WA  6907       
mailto:paul@physics.uwa.edu.au  AUSTRALIA                            
http://www.pd.uwa.edu.au/~paul

            God IS a weakly left-handed dice player
____________________________________________________________________



  • Prev by Date: Re: Reasonably Simple Integral Mathematica Apparently Can't Do
  • Next by Date: Re: How to do 3D plot of vertical planes?
  • Prev by thread: sorting a Matrix
  • Next by thread: Re: sorting a Matrix