MathGroup Archive 2010

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

Search the Archive

Re: Sort a List, in a List of Lists of Lists

  • To: mathgroup at
  • Subject: [mg113822] Re: Sort a List, in a List of Lists of Lists
  • From: "Nasser M. Abbasi" <nma at>
  • Date: Sun, 14 Nov 2010 06:10:08 -0500 (EST)
  • References: <ibl9g2$b8a$> <>
  • Reply-to: nma at

On 11/13/2010 3:32 PM, Nasser M. Abbasi wrote:

> one way:
> ------------------
> a={{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}};
> {nRow,nCol,nPages}=Dimensions[a];
> Map[Sort,Flatten[a,1]];
> Partition[%, nCol]
> ------------------
> Out[138]= {{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}}

ps. In the above, {nRow,nCol,nPages}=Dimensions[a] should really be  {nPages,nRow,nCol}=Dimensions[a];

If you really want to do this in a more 'obvious' way, you can simply Sort each row in each page
by 'hand' as in

Table[ Sort[ a[[k,j]] ], {k,1,nPages},{j,1,nRow}]

Out[407]= {{{1,2},{1,2},{1,1}},{{1,1},{1,1},{1,2}},{{1,2},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{1,2},{1,2}},{{1,2},{2,2},{2,2}}}

There is nothing wrong with this. It says go over each page in
the 3D structure, sort each row. The nice thing is that the
result will have  the same shape as the original 3D matrix. So
no need to reshape it using Parition.

I think of a 3D matrix as made up of a book, the number of pages
is the size of the third dimension, and each page in the matrix has
size nRow,nCol.  nRow tells how many lines on the page, nCol tells
how many columns in the page.

nPages actually is the first entry returned by Dimensions, not the third.
so, in 3D, when writing A[[ 1,2 ]] the '1' is the page
number, and '2' will be the row number on that page.

in 2D, A[[ 1,2 ]] then '1' is the row number, and '2' is the
column number. There is only one page, so no entry for it.

For 3D, A[[1,2,3]] means page 1, row 2 on that page, column 3  on that page.

For 4D matrix, A[[ n,p,i,j ]] is  n'th book, p page in that book,mathgroup at
i'th row on that page, j'th column

For 5D matrix  A[[ m,n,p,i,j ]] is m library, n book in that library,
p page in that book, i row on that page, and j column.


If you think of the lists as the above, everything becomes very clear
and easy. At leat for me. I do not like to think of lists as nested and
levels and such. I always try to look at a list as arrays and matrices,
and look at what dimensions it has, and go from there.


  • Prev by Date: Re: 2-D Butterworth lowpass filter?
  • Next by Date: Mathematica 8 docs online now!
  • Previous by thread: Re: Sort a List, in a List of Lists of Lists
  • Next by thread: SetAttributes[CenterDot, Flat]