MathGroup Archive 2006

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

Search the Archive

Re: Sorting Rows of a Matrix


On 3/14/06 at 6:00 AM, gregory.lypny at videotron.ca (Gregory Lypny)
wrote:

>How can I sort the rows of a matrix by using one or more of the
>columns as sort keys just like in a spreadsheet?  For example, if
>matrix M is 12 x 2 with numbers in the first column and unordered
>month numbers in the second, how would I order the rows by month?

There are a number of ways to achieve what you want. One way to do it would be based on the documentation for Sort, i.e.,

Sort[M, OrderedQ@{#1[[2]],#2[[2]]}&]

That is use the second argument for Sort to specify the sort order.

Equivalent results can be obtained with

M[[Ordering[M[[All,2]]]]]

to order by the second column

I find for me, this second method runs faster for large arrays.
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: ShadowPlot3D
  • Next by Date: Re: Sorting Rows of a Matrix
  • Previous by thread: Re: Sorting Rows of a Matrix
  • Next by thread: Re: Sorting Rows of a Matrix