Order a list based on values in another list

Mar 17, 2012 at 8:39pm

Order a list based on values in another list

Hi, I’m having a problem trying to reorder the list on the right(in the patch) based on the order of the list on the left(in the patch).

I don’t want to have to unpack anything because this process needs to work on any list length.

Would [zl lace] or [vexpr] be of any use or should I look into doing this in jitter?

– Pasted Max Patch, click to expand. –
#62446
Mar 17, 2012 at 8:51pm

I should explain that the list on the right (L R) is the lift side and right side of a shape detected from a webcam. The list on the left(I, X) is the X vale of the center of the object.
The X value is already ordered based on which object is detected first but the left and right values are not ordered yet.

#225571
Mar 17, 2012 at 8:59pm

This probably explains it a little better:

– Pasted Max Patch, click to expand. –
#225572
Mar 17, 2012 at 9:02pm

zl nth

#225573
Mar 17, 2012 at 10:14pm

Hmm, I’m not sure that will help.

I’ve made another patch. This one is an attempt to test each X value against the L and R values.

So I want data to come out like this:

xvalue 1
lr value 1
lr value 2
lr value 3
xvalue 2
lr value 1
lr value 2
lr value 3

Then I need to find a way to order the list of lr values based on whether x is both greater than L and less than R.

Unfortunately printing to the max window shows the values coming out in the wrong order.

Here is the new patch:

– Pasted Max Patch, click to expand. –
#225574
Mar 17, 2012 at 10:29pm

wait, so you want this to work with lists “of any length”, but the data is in sets of four values, right? xval Ir Ir Ir sets.

You need to explain what you’re expecting to get out a lot better. Also, you’re not telling us what you want changed about the patch(es) you post here – you say you don’t want to unpack, yet I see unpack and iter all over the place :-/

#225575
Mar 17, 2012 at 10:43pm

Sorry my explanation wasn’t great, I was having trouble working out exactly what I needed to do to get to the result.

Hopefully this next patch will illustrate it better.

If you press the button marked ‘Go’ you should see in the max window four rows of data.

0 0 0 0 5 15 0 0
0 0 0 0 0 0 85 95
15 25 0 0 0 0 0 0
0 0 35 45 0 0 0 0

As you can see the patch is looking for whether the X value is somewhere between the L and R values.
It then has placed the L and R values in the list based on which place the X value was in the list.

My problem now I guess, is getting rid of the 0′s while retaining the position of the L and R values.

So from the above list of values I want to get one list:
15 25 35 45 5 15 85 95

any ideas on how to achieve this would be great.
Also any ideas on a simpler way of achieving this would be great.

As I said before this should work on any amount of X values and pairs of LR values.

Let me know if I’m still making no sense.

– Pasted Max Patch, click to expand. –
#225576
Mar 17, 2012 at 10:56pm

Got it working now.

Can’t help but think that it is a little more complicated that it needs to be though.

– Pasted Max Patch, click to expand. –
#225577
Mar 17, 2012 at 10:58pm

Doing the close and reopen patch test seems to show that it only works after the 2nd press of the Go button.
Can’t seem to figure out why.

#225578
Mar 18, 2012 at 1:17am

I haven’t seen the patches (no Max here) but you might try [zl filter] if you haven’t, gets rid of the 0′s and gives you index numbers for everything. might be helpful somewhere…

#225579
Mar 19, 2012 at 12:25am

If you [substitute] the occurrences of each 0 for a 1 then you can multiply the four lists item by item using [vexpr] and you’ll have the desired output list.

#225580
Mar 19, 2012 at 1:13am

or keep the zeros and vexpr +

#225581
Mar 19, 2012 at 10:31pm

Vastly improved Terry, I’m rusty!

#225582

You must be logged in to reply to this topic.