Forums > MaxMSP

replace list elements with sequential $ args

September 21, 2013 | 4:54 pm

I need to replace all the 1′s in an input bit list with sequenced changeable arguments that maintain the position of the 1′s in the original list.

ex.

in: 1 0 1 0 1 1
out: $1 0 $2 0 $3 $4

in: 0 0 1
out: 0 0 $1

[regexp "1" @substitute $] gives me the $ in the right places, but of course they aren’t numbered. How do I modify that expression or use sprintf to generate the desired output?

-

– Pasted Max Patch, click to expand. –

-

The purpose of the changeable arg output messages is to generate weighted higher-order binary sequences based on lower-order binary sequences. In other words, if I input all 2^4 bit lists (0 0 0 0 to 1 1 1 1) into [$1 $2 0 0 $3 $4], it will output all possible changes between 0 0 0 0 0 0 and 1 1 0 0 1 1 because 1 1 0 0 1 1 has a weight of 4.

ex.

in: 1 0 1 0
out: 1 0 0 0 1 0

in: 0 0 1 1
out: 0 0 0 0 1 1

etc.

I need to do this with lists of variable lengths and weights. If there is an easier way to do this sort of thing, I’m all ears…


September 21, 2013 | 8:20 pm

Man.. this is making my freakin’ brain melt and my entire project has come to a grinding halt because of it.

Does anyone know if it’s even possible to create dynamic lists of changeable arguments? Max just isn’t having it.


September 21, 2013 | 9:56 pm

Here’s one way, I’m sure there are others…

<code>

– Pasted Max Patch, click to expand. –

</code>


September 21, 2013 | 10:59 pm

THANK YOU. Very slick. I have been working on this problem for an obscene amount of time.. and simply unable to get it working. You totally saved my ass.

btw, I was able to modify the patch to make it significantly more efficient – 5 or 6 times faster – so I thought I’d post it. It’s still your brainchild.

If anyone else has any other approaches for formatting multiple changing args in a message box, please post them. It’s a surprisingly challenging problem.

-

– Pasted Max Patch, click to expand. –

September 22, 2013 | 2:44 am

Hello,

This is just a minor tweak really, but if you reset the counter on each run, you don’t have to bother with calculating the number of 1′s in the original list.

<code>

– Pasted Max Patch, click to expand. –

</code>

best

Richard


September 22, 2013 | 3:23 am

You dropped another 4 objects and made it even faster.. more than 8 times faster than the original. It’s pretty amazing what a few tweaks can do for speed and efficiency.


September 22, 2013 | 10:54 am

One alternate way: instead of figuring out what you need to generate (e.g. Substitution), you just brute force and filter.

Will post an example later, but it probably is a lot simpler. It’s basically comparing the sums


September 22, 2013 | 11:19 am

tosymbol… never knew that existed, thanks!


September 22, 2013 | 11:46 am

without going into the details of the given problem, but, when working with 2-bit style lists, dont forget the two wonderful objects [pak] and [zl nth].


September 22, 2013 | 9:49 pm

@peter- post it!

@Roman – It wasn’t so much the bits as the args that were messing with me. I couldn’t get a message box to take $ input in list form. Obviously the $ character is read as a changeable arg by Max, but I thought if a character was formatted as a symbol Max didn’t treat it as a meta character.. yet no matter how I packaged it – preceded by a slash, in quotes, passing it through [tosymbol], etc. Max wouldn’t play nice. I’m still not entirely clear why the above patches work while some of my attempts did not.

This is the kind of stuff I was doing:

– Pasted Max Patch, click to expand. –

September 23, 2013 | 7:08 am

<code>

– Pasted Max Patch, click to expand. –

</code>


September 23, 2013 | 1:39 pm

One potential issue with using substitution is that your $1 $2 $3 values only go up to 9. Don’t know if that matters for your use, but it might.

What about storing matches into coll?


September 25, 2013 | 3:35 am

Ahh.. another working example. Nice.

Peter, yes I need to do this for long lists. I already created a working patch utilizing the substitution method which slices long lists and then rejoins them. Maybe not ideal, but it works and I don’t know how else to approach it.

I’m not sure how I would implement colls to derive the same output. There are many, many permutations for the lists I’m processing. It’s not possible to store them all.

Without going into too much detail, I’m treating each list as a vertex in a higher dimensional lattice and I want to calculate the various lower dimensional structures of that space. For example, a 6-bit list encodes a single 0d vertex of a 6-cube (just as 3-bit lists encode the corners of a cube), 1d edges encode a single bit change between two lists, 2d faces (4 lists) encode 2-bits of difference, 3d cells (eight lists) encode 3-bits of difference, and so on.

So I need to freely map and scale between short and long lists of different lengths on the fly. I’m using coll to store the output, but I don’t see how I would use coll to generate it…


September 25, 2013 | 5:57 am

One possible way of using coll

<code>

– Pasted Max Patch, click to expand. –

</code>


September 26, 2013 | 2:59 am

Hey thanks Rick.. though I think that Peter was talking about a method that doesn’t involve substitution with $ – to make it easier to handle long lists. Either way, I got it worked out. Thanks for all the input everyone.


September 26, 2013 | 8:29 am

Yeah, instead of doing substitution with $1 etc. store the pairs into a coll and do a per instance lookup. The filter way would be to generate and then use vexpr to compare, then see if the sum of the list matched the desired result.

All this said, this sounds like a really great place for some imperative programming in Java, C++, Javascript…Clojure would be particularly fun since it can handle infinite sequences.

Looking forward to seeing the results!


September 27, 2013 | 10:04 pm

maybe the patch from richard works, because zl is not working with "real" lists? in your attempt there is a [join] and a [t l] after the [sprintf].


September 28, 2013 | 3:25 am

That’s interesting, Peter. I have friends telling me that I’m a Lisp guy only I don’t know it. Seemed like a complement, but I couldn’t tell for sure. ha!

Roman, very curious… I need take a closer looking at join and the zl objects because I have no idea what you mean by "real lists".


Viewing 18 posts - 1 through 18 (of 18 total)