Replacing multiple numbers in a list at once

Mario Fernando Cardoso's icon

Hi!

I have a list of ceros (0 0 0 0 0 0) and a list of random numbers.
I want to replace 0 with 1 on each position indicated by the random numbers.
For example, if I have (0 0 0 0 0 0 0 0) and I have a list of random numbers (0 5 3 6), those random numbers would indicate at which positions in the list to replace a zero with a one... The result should look like this (1 0 0 1 0 1 1 0) because a 0 was replaced with a 1 in the positions 0, 3, 5 and 6 of the list.

I tried using zl.nth and zl.iter but I can't seem to understand how to use them in this case.

I'm looking for something like the zl.nth replace feature but replacing multiple elements at once.

Any ideas, suggestions or guidance will be very much appreciated!

Max Patch
Copy patch and select New From Clipboard in Max.

Thanks!
Merry Christmax and Happy New Year to everyone!

Mafo.

Roman Thilenius's icon

sounds like a coll job. or why don´t you run a counter though a route.

for multiple replace messages, what is wrong with "0 5 3 6" -> [iter] -> [prepend replace] ?

Sébastien Gay's icon

I came-up with this (works with index starting at 1) :

But, understanding that your need may be considered as generating a list of a given size, containing 0 or 1 randomly-chosen values, this may also work :

Or, even :

Iain Duncan's icon

This kind of thing is easy with a handful of lines of code in Scheme for Max, in case you are interested in using an external and code to do it. I can post code if you'd like, just let me know. Scheme for Max hosts a Scheme (lisp) interpreter in Max, which makes list processing very straightforward.

Mario Fernando Cardoso's icon

Thanks for your help!

I ended up using a solution that was suggested to me in the Max/MSP facebook group using coll and a "nsub 0 $1 1" message...

My goal is to use this to randomly fill the steps in a sequencer I'm developing, so that was my best option...

Here is a quick implementation that I added to my patch in case it is useful to someone:

Max Patch
Copy patch and select New From Clipboard in Max.


Thank you all and Happy Holidays!