Jump to content
Software FX Community
Sign in to follow this  
marco.shaw

Calculating a median of values

Recommended Posts

An average are not always the best statistical value when dealing with a set of numbers.  Here's a way to determine the median of some values: 

$array=(gc ($args)|foreach{[int]$_}|sort) if($array.count%2 -eq 0)

{write-host median: (($array[($array.count/2)-1]+$array[$array.count/2])/2)}

else{write-host median: $array[($array.count/2)-0.5]}

The file containing the number to get the median from is provided as an argument when calling the script.  The input file should be of the format:

10

20

300

30

etc.

(I haven't quite figured out how to handle the line wrapping with posts here.)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...