If I have a 2-D array, is there an easy way of collecting the
maximum values of each column using LOOP? MAXIMIZE works fine when
the number of columns is fixed (and it's possible to explicitly code
a MAXIMIZE instruction for each column), but I haven't been able to
figure out what to do when it isn't.
--
Ola Rinta-Koski ···@cyberell.com
Cyberell Oy +358 41 467 2502
Rauhankatu 8 C, FIN-00170 Helsinki, FINLAND www.cyberell.com
"Ola Rinta-Koski" <···@cyberell.com> wrote in message
···················@arenal.cyberell.com...
> If I have a 2-D array, is there an easy way of collecting the
> maximum values of each column using LOOP? MAXIMIZE works fine when
> the number of columns is fixed (and it's possible to explicitly code
> a MAXIMIZE instruction for each column), but I haven't been able to
> figure out what to do when it isn't.
Hi,
how about:
(loop for j below col
collect (loop for i below row
maximize (aref array i j)))
as in
> (let ((col (+ 2 (random 5)))
(row (+ 2 (random 5))))
;;make an array of pseudo-random dimensions
(let ((array (make-array (list row col))))
;; fill with random numbers
(dotimes (i row)
(dotimes (j col)
(setf (aref array i j) (random 50))))
;; print so we can visually verify
(print array)
;; return the list of max values for each column
(loop for j below col
collect (loop for i below row
maximize (aref array i j)))))
#2A((28 8 26 29)
(30 5 13 41)
(33 13 33 43)
(7 16 18 43))
(33 16 33 43)
>
...........
coby
"Coby Beck" <····@memetrics.com> writes:
> "Ola Rinta-Koski" <···@cyberell.com> wrote in message
> ···················@arenal.cyberell.com...
> > If I have a 2-D array, is there an easy way of collecting the
> > maximum values of each column using LOOP? MAXIMIZE works fine when
> > the number of columns is fixed (and it's possible to explicitly code
> > a MAXIMIZE instruction for each column), but I haven't been able to
> > figure out what to do when it isn't.
> how about:
> (loop for j below col
> collect (loop for i below row
> maximize (aref array i j)))
Of course - thanks a lot...
--
Ola Rinta-Koski ···@cyberell.com
Cyberell Oy +358 41 467 2502
Rauhankatu 8 C, FIN-00170 Helsinki, FINLAND www.cyberell.com