HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Median Value?

11-13-2008, 04:38 AM#1
Limb_Smasher
Hey I'm still new to the JASS language, but I was wondering if there was a way to track a median value for a set of integers (or reals).

In my map I've got a set of 8 integers, and it would be helpful if I knew the median value. The only thing I can come up with is using a bunch of if-then statements. I haven't actually coded it, but I imagine it getting pretty ugly.

Does anyone know of a way to get the median value of a set of integers?
11-13-2008, 04:57 AM#2
Here-b-Trollz
Assuming you are continuously sorting them:

Collapse JASS:
if(R2I(I2R(integercount)/2)==I2R(integercount)/2)then //or something... IF IT'S EVEN:
    return (stack[integercount/2]+stack[integercount/2+1])/2
else //it is odd:
    return stack[R2I(I2R(integercount)/2+.5)]
endif

I'm not actually sure that all the R2I's and such are needed... also there's probably an easier way to do it, but this is my attempt.
11-13-2008, 08:04 AM#3
Limb_Smasher
No no, im sorry maybe I worded it funny.
I didn't mean the mean value. I meant the median.

The number thats in the middle. Take for example the set
{1, 6, 9, 11, 75}

the median is 9 because its the middle value.

anyway to locate that integer without creating a BUNCH of if-then statements?
11-13-2008, 09:08 AM#4
Pyrogasm
No, you need a bunch of if-then statements. However, you sort them as numbers are added to the set so that there are less to do at any given time.
11-13-2008, 12:33 PM#5
Here-b-Trollz
If all of your numbers are sorted from least to greatest, then the above way should work (mine).
11-13-2008, 02:39 PM#6
Anitarf
Quote:
Originally Posted by Limb_Smasher
No no, im sorry maybe I worded it funny.
I didn't mean the mean value. I meant the median.
Quote:
Originally Posted by Wikipedia
In probability theory and statistics, a median is described as the number separating the higher half of a sample, a population, or a probability distribution, from the lower half. The median of a finite list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one. If there is an even number of observations, the median is not unique, so one often takes the mean of the two middle values.

If your list is sorted, Here-b-Trollz's code does exactly what you say you want. You could indeed shave a few I2Rs from it, though.
Collapse JASS:
if (count/2)*2==integercount then
    return (stack[count/2-1]+stack[count/2])/2
else
    return stack[count/2]
endif

This is assuming your values are sorted and stored in an array under indexes from 0 to count-1.
11-13-2008, 04:59 PM#7
Limb_Smasher
Oh ok thanks,
I'll try it out

+rep
11-14-2008, 08:59 PM#8
Strilanc
http://en.wikipedia.org/wiki/Selection_algorithm