4
$\begingroup$

I have a matrix m

m={{10,2},{20,3},{30,4},{40,5},{50,6}}

I also have a list n

list1={1,2,3,4,5}

and another matrix

p={{25,-23},{64,33},{77,74},{99,5},{200,68}}

I can find certain elements in matrix m with this statement

list2=Select[m,(10 < #[[1]] < 50 ) && (#[[2]] < 6) &]

and I get

{20,3},{30,4},{40,5}

I can also use

list2=DeleteCases[m , _?(10 < #[[1]] < 50 && #[[2]] < 6 &)];

Now, how can I get the position of those deleted items (or selected items) so I can use that information to trim down both the list1 and other matrix p to yield

{2,3,4}

and from matrix p

p={{64,33},{77,74},{99,5}}

I've tried to obtain position information about the deleted items using:

list3 = Position[m, _?(10 < #[[1]] < 50) && (#[[2]] < 6) &]

and

list3 = Position[m, (10 < #[[1]] < 50) && (#[[2]] < 6) &]

but I'm getting empty set all the time.

Thanks,

$\endgroup$
1
  • $\begingroup$ You have an operator precedent issue. Use Position[m, _?((10 < #[[1]] < 50) && (#[[2]] < 6) &)] instead. Looking into why this gives messages. $\endgroup$
    – Edmund
    Commented Apr 14, 2017 at 1:28

2 Answers 2

2
$\begingroup$

One way:

selected = 10 < #[[1]] < 50 && #[[2]] < 6 & /@ m;
Pick[p, selected]

{{64, 33}, {77, 74}, {99, 5}}

Pick[list1, selected]

{2, 3, 4}

Of course, you can also use this on m: Pick[m, selected].

To make your approach work you have to change a few things. As Edmund said, the & needs to go inside the parenthesis. Then you need another set of parenthesis to surround the And statement. You also need to set Heads -> False or it will try to match your pattern with the heads of subexpressions, in this case List. That will give errors because List doesn't have a first and a second part. Finally you also need to specify at what level the subexpressions exist. All in all:

selected = Position[m, _?((10 < #[[1]] < 50) && (#[[2]] < 6) &), {1}, Heads -> False]
Extract[p, selected]

{{64, 33}, {77, 74}, {99, 5}}

Of course, this is based on the premise that you want to use the pattern in the question. As Edmund has shown, there are more appropriate patterns.

$\endgroup$
1
  • $\begingroup$ OK - yes that works great. without the {1}, and Heads->False, it is a no-go. Thanks! $\endgroup$ Commented Apr 14, 2017 at 1:59
2
$\begingroup$

You may use a named Pattern with a Condition to get the Positions.The use Extract to collect them from p.

pos = Position[m, (v : {a_, b_}) /; (10 < a < 50 && b < 6)]
{{2}, {3}, {4}}

Then

Extract[p, pos]
{{64, 33}, {77, 74}, {99, 5}}

Hope this helps.

$\endgroup$
2
  • $\begingroup$ Thank you - this works too. Can you recommend a place to read up on this syntax? I'm very unfamiliar with the /;. $\endgroup$ Commented Apr 14, 2017 at 2:01
  • 1
    $\begingroup$ @TomMozdzen I have given links in the answer. Click on the function name or search the help in Mathematica or highlight the function and press F1 in Mathematica. Note that you can evaluate code in the help and add additional parameters in the help as well; it is interactive. $\endgroup$
    – Edmund
    Commented Apr 14, 2017 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.