How to sum elements from different lists
Hello,
I want to sum the elements from 2 lists, and I want to know which is the best way.
For example, imagine the first list is 1 9 2 and the second one is 8 4 6, so I need to sum 1+8, 9+4, 2+6.
There is no zl object to do this. I have tried to create a JavaScript object but it has problems while reading lists.
Thank you very much.
Have a look at the vexpr object.
The list stuff was tricky (kind of, you can't just take a list x, and output it as a list). I don't know if there's a built in method for doing what I did here:
autowatch = 1;
inlets = 2;
outlets = 1;
var list_1 = [];
var list_2 = [];
var output = [];
function list(x){
if(inlet == 0){
for(i = 0; i < arguments.length; i ++){
list_1[i] = arguments[i];
}
for(i = 0; i < arguments.length; i++){
output[i] = list_1[i]+ list_2[i];
}
outlet(0, output);
}
if(inlet == 1){
for(i = 0; i < arguments.length; i ++){
list_2[i] = arguments[i];
}
}
}
But this seems to work.
EDIT: also, the vexpr object ;)
Why not the simple way?
@Patrick Delges thank you! I knew expr but not vexpr. It worked perfectly
@evan thank you, working with lists with JavaScript in Max seems to be a little messy. The max js documentation is not 100% clear. Do you know if there is a book focused on js for max?
@florian1947 thank you, but the Idea was to work with dynamic lists, so pack/unpack is not useful in this case.
Thank you very much for your help!
For the fun of zl objects, got this one without vexpr. And the lists can have variable lengths.
@Stephane Morisse That one is cool! Is it better to use vexpr in terms of performance and RAM use? I am trying to optimize my patch.
Thank you!
TBH, I've got no idea. Vexpr is by far the simpler one-object solution but you've got to unpack the result and if you want to deal with variable lengths list, you'll have to work out a solution with another object than unpack.