MathGroup Archive 1998

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

Search the Archive

Sorting of Matrixes and Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg14454] Sorting of Matrixes and Lists
  • From: David Lincoln <dlincoln at ici.net>
  • Date: Wed, 21 Oct 1998 03:32:51 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

In response to a recent question on how to sort a matrix here are some
methods
for different possible criteria of sorting matrixes and lists.


1) Sort columns of a matrix in ascending order based on the columns
second entry.
    This uses same method as sorting by second entry of rows with the
addition
    of a before and after transpose.

 m = { { 6,9,4}, {5,8, 3}, {2, 11, 7}}

Transpose[
        Sort[Transpose[m],
          (#1[[2]] <= #2[[2]])&]]

{{4,6,9},{3,5,8},{7,2,11}}

2) Sort rows of a matrix by descending order according to sum of row
entries.

  n := {  {1,2,3}, {4,5,6}, {7,8,9}}

 Sort[n,(Apply[Plus,#1]>= Apply[Plus,#2])&]

 {{7,8,9},{4,5,6},{1,2,3}}

 3) Given a list v  of real numbers and a real number c sort in
ascending order according to
distance from c.


v:= { 1.2 , 6.3, 10.4,20.4,31.1,44.2,55.6,88.3} c:= 33.2


 Sort[v,(Abs[c - #1] <= Abs[c - #2])&]
{31.1,44.2,20.4,55.6,10.4,6.3,1.2,88.3}



  • Prev by Date: Re: FactorInteger
  • Next by Date: Re: Corrupted NB
  • Previous by thread: contour plots from non-rectangular matrices
  • Next by thread: Partial differential equation