From: Drew Krause
Subject: sorting columns in multi-d arrays?
Date: 
Message-ID: <3BCE1D53.6568C6DD@mindspring.com>
I'm banging my head against this problem, mostly (I think) because of
incompatible data types (ie, 'sort' applies only to sequences, but how
to make an array into a sequence?).

Anyway, if anyone has a quick-&-dirty way to sort columns of any array
by the top member so that, e.g.

2 1 3
0 7 4

becomes

1 2 3
7 0 4

I'd sure appreciate it. Thanks for any help!

Drew Krause

From: Marco Antoniotti
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <y6c8ze9n5h6.fsf@octagon.mrl.nyu.edu>
This should do...  (not much tested and probably not the best way of
doing this).  Actually it should sort given any ROW.

(defun sort-2D (a-2D-array &key (row 0) (test #'<)) ; Add :KEY if needed.
  (let* ((rows (array-dimension a-2D-array 0))
	 (cols (array-dimension a-2D-array 1))
	 (indices (make-array cols))
	 (result (make-array (list rows cols)
			     ;; add the :element-type etc here...
			     ))
	 )
    ;; Create a vector of "indices": a mapping from first row elements
    ;; to their positions.
    (loop for col from 0 below cols
	  do (setf (aref indices col) (list (aref a-2D-array row col) col))
	  finally (setf indices (sort indices test :key #'first)))

    ;; Fill in the result.
    (loop for (value column) across indices
	  for target-column from 0
	  do (loop for target-row from 0 below rows
		   do (setf (aref result target-row target-column)
			    (aref a-2D-array target-row column))))
    result))

Cheers


-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Marco Antoniotti
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <y6c4roxn5eu.fsf@octagon.mrl.nyu.edu>
...did I just help somebody doing his homework? :}

Cheers

-- 
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.
From: Drew Krause
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <3BCE403A.70E54997@mindspring.com>
Nope -- just a slow learner (though I can't speak for who's reading this
thread ...). Thanks for your help!

Marco Antoniotti wrote:

> ...did I just help somebody doing his homework? :}
>
> Cheers
>
> --
> Marco Antoniotti ========================================================
> NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
> 719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
> New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
>                     "Hello New York! We'll do what we can!"
>                            Bill Murray in `Ghostbusters'.
From: Coby Beck
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <Iwpz7.353651$aZ.72233848@typhoon.tampabay.rr.com>
"Drew Krause" <········@mindspring.com> wrote in message
······················@mindspring.com...
> I'm banging my head against this problem, mostly (I think) because of
> incompatible data types (ie, 'sort' applies only to sequences, but how
> to make an array into a sequence?).
>

A vector is a sequence....

UDT 158 > (setf arr #(1 6 3 2) )
#(1 6 3 2)

UDT 159 > (sort arr #'>)
#(6 3 2 1)


Coby

--
(remove #\space "coby . beck @ opentechgroup . com")
From: JP Massar
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <3bce2623.244374532@news>
On Wed, 17 Oct 2001 17:07:47 -0700, Drew Krause
<········@mindspring.com> wrote:

>I'm banging my head against this problem, mostly (I think) because of
>incompatible data types (ie, 'sort' applies only to sequences, but how
>to make an array into a sequence?).
>
>Anyway, if anyone has a quick-&-dirty way to sort columns of any array
>by the top member so that, e.g.
>
>2 1 3
>0 7 4
>
>becomes
>
>1 2 3
>7 0 4
>
>I'd sure appreciate it. Thanks for any help!
>
 
Write a routine that transforms the 2-d array into a list of vectors,
each vector being a column of the original 2-d array.  E.g.,

2 1 3
0 7 4   -->   (#(2 0) #(1 7) #(3 4))

Now sort the list, using the first element of each vector as the key.

Now write the inverse routine that transforms the list of vectors back
into a 2-d array (or just put the reordered data back into the
original array)
From: Jeff Greif
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <vZsz7.16961$uD4.6766808@typhoon.we.rr.com>
Transpose the matrix.  Sort the rows with a key of (lamba (row) (aref
row 0)).
Transpose back.

Jeff

"Drew Krause" <········@mindspring.com> wrote in message
······················@mindspring.com...
> I'm banging my head against this problem, mostly (I think) because of
> incompatible data types (ie, 'sort' applies only to sequences, but how
> to make an array into a sequence?).
>
> Anyway, if anyone has a quick-&-dirty way to sort columns of any array
> by the top member so that, e.g.
>
> 2 1 3
> 0 7 4
>
> becomes
>
> 1 2 3
> 7 0 4
>
> I'd sure appreciate it. Thanks for any help!
>
> Drew Krause
>
From: Janis Dzerins
Subject: Re: sorting columns in multi-d arrays?
Date: 
Message-ID: <87u1wxmhja.fsf@asaka.latnet.lv>
"Jeff Greif" <······@spam-me-not.alumni.princeton.edu> writes:

> Transpose the matrix.  Sort the rows with a key of (lamba (row)
> (aref row 0)).  Transpose back.

Could you not do the same with columns:

(lambda (col) (aref col 0)) ?

(No, you can't. The matrix in this case is not a vector af vectors but
a 2D array.)

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.