Sum list numbers and compare to another value?
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.
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!
-- Pasted Max Patch, click to expand. --Copy all of the following text. Then, in Max, select New From Clipboard.----------begin_max5_patcher---------- 1373.3oc0ZssaaiCD8Y6uhoFKv9h2.QRQI4EnEX+C122VTHKQmvBYJCIpMYa Q+2WdQ9RRsiIkkXSeIAhhgZ3gyblyLLea9rEqqeh0t.9S3efYy917YyLCoGX V+yyVrM+ohp7VyzVTTucKSHWrz9NI6IoYbxc.WrqSBU71CucSsPJx2xLy3uZ 34U6eytFVqZYxk7ZwmaXERqMPRStitDvY2EsDh5+A7oSVtV9WMKGBqda+ZkK KdfKt+LqShdIRLqF9zURzskKpXRydB0OHuzXm0q+xePQKNNy5N49oFoG76ym q+wxaDwv2MXXBiSMaojaDlvXxQXBi7GlhldXB4.L49NFQHGQtAriiWM8637h htscU4RFTU+HqUB+adUmJLcDwgXbrduSnF3.Qi7GIRGOjXKqsM+d1OfDespn hk2b8Mt86K+ucL61awhC6iKDBESLG9njU2XLDJxrBHhIjLlZdH6R.I9L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 ScA6IMX1i99q+0ydB34E1A64EF8jhOtXO5JydSgOg67hPcvdxBm4j5B7D8LN pI0dvusB2cBeLF8qgOa4k6p4BYeNhXjIyTB1VcA8LOQo1qJkjc3oiFw3R1e0 jOgKXU2FKk7gW2dvOeRSJ4gKbYIgEdPtvshBm87FhqGk4f8fC24k4t9byfBy AlS3yUfmWvkQszVDDx1k1re7Ijss1jrjCOMEbYNDajEtPCWRyiBWddWPmvQb 3jlrvcVMBQEim0j5jmy.MG3Sy+97+G367i8O -----------end_max5_patcher-----------
You are a star sir, whatever works! Will have a look at this first thing tomorrow (it’s 1am here)…thanks Holland :)
Forums > MaxMSP