How to calculate the Mode
Hi all
I have a number coming from wekinator that is detecting 5 different patterns. Wekinator as an output gives a number fluctuating from 1 o 5.
How can I know the Mode if this number in Max? I need to know what is the most repeated number in a row of 15 values.
t b i
route 1 2 3 4 5
and then counter or accum?
Hi Roman
I need it to run in realtime, something more close to [zl.group 15] and then [zl.median] but instead of median I need Mode.
Max Patch
Copy patch and select New From Clipboard in Max.
I resolve it with js
Does someone have a better solution?
var myval=0;
if (jsarguments.length>1)
myval = jsarguments[1];
function mode(numbers) {
// as result can be bimodal or multi-modal,
// the returned result is provided as an array
var modes = [], count = [], i, number, maxIndex = 0;
for (i = 0; i < numbers.length; i += 1) {
number = numbers[i];
count[number] = (count[number] || 0) + 1;
if (count[number] > maxIndex) {
maxIndex = count[number];
}
}
for (i in count)
if (count.hasOwnProperty(i)) {
if (count[i] === maxIndex) {
modes.push(Number(i));
}
}
if(modes.length <= 1) modes.push(-1);
return modes;
}
function list()
{
var a = arrayfromargs(arguments);
//post("received list " + a + "\n");
myval = mode(a);
bang();
}
function bang()
{
outlet(0,myval);
}
The javascript will work, but if you want an all-max solution you might look at histo (example below). Also, I posted code several weeks back for a time-based histogram (where entries expire after X ms) if you're interested.
Max Patch
Copy patch and select New From Clipboard in Max.
Hi Peter
Very nice how you resolve it on [p ZL_version]. Thanks!