I would like to get an average value of an array.
Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)
One condition is, I don't know the total length of an array because it will
be changed every time. Maybe I could write down as (although it looks
complicated..),
(setf total-value 0)
(loop for i from 0 to arr-index
do (setf total-value (+ total-value (aref arr i)))
finally (setf ave-value (/ total-value (+ arr-index 1))))
However, I just wonder that is there any function or macro which can add
whole values in an array? I want use this function with one simple line.... I
couldn't find that kind of things, but maybe something exist? Thanks for
advance. =)
Sungwoo
Sent via Deja.com
http://www.deja.com/
> (setf total-value 0)
> (loop for i from 0 to arr-index
> do (setf total-value (+ total-value (aref arr i)))
> finally (setf ave-value (/ total-value (+ arr-index 1))))
This seems simpler:
(setf ave-value (/ (loop :for i :from 0 :below array-length
:summing (aref arr i))
array-length))
Or, perhaps you could LOOP ACROSS the array, if it doesn't have extra
elements.
> I would like to get an average value of an array.
LOOP does well at taking sums, and so the average of an array can be
expressed as:
(/ (loop for x across <array> sum x)
(length <array>))
--
Frode Vatvedt Fjeld
> I would like to get an average value of an array.
>
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)
If the array you have is vector (ie. one dimensional), you can use
REDUCE:
cl-user(3): (defun average (arr)
(/ (reduce #'+ arr)
(length arr)))
average
cl-user(4): (average #(1 2 3 4 5 6 7 8 9 10))
11/2
cl-user(5):
> However, I just wonder that is there any function or macro which can add
> whole values in an array?
Is REDUCE what you want?
Janis Dzerins
--
If million people say a stupid thing it's still a stupid thing.
> Hello,
>
> I would like to get an average value of an array.
>
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)
(/ (reduce #'+ R)
(length R))
Watch out for zero-length arrays.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
> Hello,
>
> I would like to get an average value of an array.
>
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)
>
> One condition is, I don't know the total length of an array because it will
> be changed every time. Maybe I could write down as (although it looks
> complicated..),
do you mean an array or do you mean a list?
because if it's really an array, you can get its dimension easily.
reduce #'+ can sum a vector.
(defun vector-average (one-dim-array)
(let ((n (array-dimension vector 0)))
(/ (reduce #'+ vector) n)))
this can be extended to handle more dimensions, but you get the idea.
> (setf total-value 0)
> (loop for i from 0 to arr-index
> do (setf total-value (+ total-value (aref arr i)))
> finally (setf ave-value (/ total-value (+ arr-index 1))))
>
> However, I just wonder that is there any function or macro which can add
> whole values in an array? I want use this function with one simple line.... I
> couldn't find that kind of things, but maybe something exist? Thanks for
> advance. =)
>
> Sungwoo
>
>
> Sent via Deja.com
> http://www.deja.com/
--
J o h a n K u l l s t a m
[kull...@ne.mediaone.net]
sysengr
> Is REDUCE what you want?
yes, it give me right answer. I little bit worried about the meaning of the
word 'REDUCE'. If 'REDUCE' function actually reduce the array size, it is not
suitable for me... But after took a look the reference my silly worries gone.
=) Thanks =)
I should give you the hints and the pointers, but I'm tired, so here's
how you compute the average of a vector of numbers:
(/ (reduce #'+ <vector>) (length <vector>))
Incidentally, the standard deviation is also very useful if you ever
work with averages:
(let* ((mean (/ (reduce #'+ <vector>) (length <vector>)))
(sdev (sqrt (/ (reduce #'+ <vector> :key #'(lambda (elt) (expt (- elt mean) 2)))
(1- (length <vector>))))))
...)
As you may have noticed, this is idiomatic Common Lisp for the sum
operator.
#:Erik
--
The United States of America, soon a Bush league world power. Yeee-haw!
> do you mean an array or do you mean a list?
I mean 'ARRAY' as I wrote. =)
>
> because if it's really an array, you can get its dimension easily.
> reduce #'+ can sum a vector.
>
> (defun vector-average (one-dim-array)
> (let ((n (array-dimension vector 0)))
> (/ (reduce #'+ vector) n)))
>
Thanks, =)