If statements with mathematical operations?
Hi I'm new to Max Msp and today I ran into a brick wall when trying to do something rather simple.
I wanted to make an if statement that takes every even number from a flow on incoming inputs and turn them negative number. I made an if statement that looks something like this: [if $i1 % 2 == 0 then $i1 * -1 else $i1]. I didn't give me any errors but it didn't do the operation either so i didn't work. Is there any way to do something like this? I simply want to look for a certain values and do a math operation on them, but I don't want to use the [sel] object as I feel it is not smart for large lists of numbers.
I also tried to make the numbers into a list and then take every second item in that list but I couldn't figure out how to access individuals elements in a list and process them (even after reading documentation and looking at zl objects).
I spend all day at this, writing here is sort of last resort. Fairly simple task, I just suck at Max for now. Bestow your wisdom upon me please.
Thanks a lot!
This should get you going...
Here's another useful approach.
calculations are not possible on the output side - only on the if side.
but if you are only working with numbers, one output is enough, and "no output" will not occur, you can do the same in expr.
it will also be lighter on CPU.
expr ( (($i1 % 2) == 0) * $i1 ) + ( (($i1 % 2) != 0) * -($i1) )
Wow I had no idea you could make conditionals expressions like that. This is great! I think the syntax is a lot less obvious than most of the other Max objects, but I don't really need the if statement when I can just make expressions like that. I was able to solve my problem now, thanks a lot.
and of course there is [vexpr] for lists...
Yes, I fumbled with that thing as well, but it does operation of every element in the list, right? Couldn't single out the elements I wanted to do stuff to, so I gave up on [vexpr], but it definitely has its uses.
since max v 4.6 it has two modes, list vs list or list vs single value: [vexpr $f1+$f2 scalarmode 1]
otherwise you could just create a list such as "5 5 5 5 5 5 5 5 5" for the second input - which but makes only sense, when the left input is not updated all the time.
i think that wasnt exactly your question but
if $i1 > ($i2 * $f3) then ($i1 + $f3) else ($f3 - $i2)
was that a question?
expr ($f1 > ($f2 * $f3)) * ($f1 + $f3) + ($f1 <= ($f2 * $f3)) * ($f3 - $f2)
...and dont mix $i and $f in [if] or [expr]