Sum list numbers and compare to another value?

Jul 19, 2012 at 1:07pm

Sum list numbers and compare to another value?

Hi all,

Apologies if I’m missing previous posts concerning this but I haven’t found an obvious solution and it’s doing my head in (I’m not great at Max), even explaining is seems difficult.

I’m trying to make a simple patch that contains a list of numbers whose sum can be filtered by another number…sort of. The patch is to contain a list of values low to high representing different duration in seconds. I’d like to be able to enter another number representing the total duration of a performance that could contain all of the values in the list whose sum is < = to the duration.

For example, if the list contains these values for seconds [1 2 3 4 5 6 7 8 9 10] and I enter 40 seconds as the performance’s total duration, I’d like the patch to output: [1 2 3 4 5 6 7 8] whose sum is 36 and not to include 9 or 10 whose sums would be 45 or 55 and thus too long etc.

I’ve looked into expre, vexpr, all of the zl objects, and posts here involving expressions, but I think I’m getting to the point of reinventing the wheel!

Attached is my experimental patch…to the left is where I’m trying to figure out how to compare the sum to the total performance and then to exclude the largest member and repeat the process etc until the result is achieved. Apologies for the messy patching and of if this is too big of a problem to sort on the forum.

Cheers,

Bill

#32307
Jul 19, 2012 at 1:08pm

damn…forgot to attach the patch…

Bill

#106059
Jul 19, 2012 at 3:24pm

Instead of lopping off the highest value until you reach your target, I started by slicing off the lowest values and accumulating them until the target is met. Not sure why I took this approach, but I hope it meets your needs!
Holland

– Pasted Max Patch, click to expand. –
#106060
Jul 20, 2012 at 12:00am

You are a star sir, whatever works! Will have a look at this first thing tomorrow (it’s 1am here)…thanks Holland :)

Bill

#106061

You must be logged in to reply to this topic.