Median calculation

Dear all,

Trying calculate the median of a list, I tried many workflows but I did not succeed. I know there are external plugins for this, but I am very curious, if and how one can define the following condition in GH to calculate the median:

If the length of the list is odd, give the item in centre
else, give the average of the two centred items

I would appreciate your ideas.
Best,
Farhang

Median calculation.gh (9.6 KB)

2 Likes

Here is a way using a Stream Filter as a kind of switch depending on if the list is odd or even.

Median Switch.gh (11.3 KB)

1 Like

Thank you very much @Dani_Abalde and @Michael_Pryor for the brilliant solutions.
What I specifically learned from you was the trick to connect 2 indexes to List Item in a way that it can result in one or two items, and the use of Stream Filter, which I was not aware of.

Best,
Farhang

And in case it would be of interest for somebody else in the forum, I updated @Michael solution based on my needs. That is:

  1. I added sorting in the beginning,
  2. Replaced Average with Mass Addition and Division, as panels cannot connect to Average directly,
  3. Simplified the odd/even detection.

Best,
Farhang


median.gh (12.2 KB)

It will be much simpler to use script component to calculate.

2 Likes