[18693] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] Sorting matrix elements, keeping track of original positions

daemon@ATHENA.MIT.EDU (Kim Elmore)
Wed Jul 21 16:51:46 1999

Date: Wed, 21 Jul 1999 15:46:37 -0500 (CDT)
From: Kim Elmore <elmore@nssl.noaa.gov>
To: S-News mail list <s-news@wubios.wustl.edu>
In-Reply-To: <Pine.HPP.3.95.990721163237.18605U-100000@jove.mps.ohio-state.edu>
Message-Id: <Pine.GSO.4.10.9907211539420.4946-100000@ev.nssl.noaa.gov>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII


	In *record* time, I recieved excellent replies, all of
which, in one way or another, do what I want.  I'm amazed by the pool of
very bright people who so willingly share their knowledge.  Thanks so
much.

	I summarize them below, but the first is very convenient for may
problem of the moment:

On Wed, 21 Jul 1999, Joseph S. Verducci wrote:

> On second thought, perhaps you want
> 
>   positioner _ function(a){
>  ro _ sapply(sort(a),function(x) row(a)[a==x])
>  co _ sapply(sort(a),function(x) col(a)[a==x])
>  cbind(ro,co)}
> 
> which gives the row and column of the sorted data
> 
> > a
>      [,1] [,2] [,3] 
> [1,]   27   24   29
> [2,]   21   22   28
> [3,]   23   26   25
> 
> > positioner(a)
>      ro co 
> [1,]  2  1
> [2,]  2  2
> [3,]  3  1
> [4,]  1  2
> [5,]  3  3
> [6,]  3  2
> [7,]  1  1
> [8,]  2  3
> [9,]  1  3

From byers@research.att.com Wed Jul 21 15:41:40 1999

## Try this,


x <- matrix(rnorm(80), ncol=8, nrow=10)
out <- cbind( c(x), c(row(x)), c(col(x)))
out[order(out[,1]),]


		### Simon Byers, AT&T Labs.



From: Joseph S. Verducci <jsv@stat.ohio-state.edu>

Is 

       matrix(rank(a),nrow(a),ncol(a))

what you want?

Example:
> a
     [,1] [,2] [,3] 
[1,]   27   24   29
[2,]   21   22   28
[3,]   23   26   25

> sort(a)
[1] 21 22 23 24 25 26 27 28 29

> matrix(rank(a),nrow(a),ncol(a))
     [,1] [,2] [,3] 
[1,]    7    4    9
[2,]    1    2    8
[3,]    3    6    5

From: Jim Hughes <hughes@diamond.cfas.washington.edu>

Kim -

Does this do what you want?

ord <- order(mat)
mat[ord] # orders the elements of the matrix mat
cbind(row(mat)[ord],col(mat)[ord]) # gives the row, col index of each
                                   # ordered element in the original matrix
Jim


		Kim Elmore, [N5OP, PP ASMEL/Glider 2232456]
"All of Meteorology is divided into three parts: Yes, No and Maybe.  The
greatest of these is Maybe." -- The original Latin appears to be garbled.


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post