TransWikia.com

How can I use Pick to select items in one list when a value in a corresponding list is > some value of another list?

Mathematica Asked on July 11, 2021

Suppose I have the following lists:

dat1 = {{43, 12.0541}, {44, 10.789}, {45, 9.505}};

dat2 = {{43, 11.60940}, {44, 10.693}, {45, 9.764}};

How can I implement Pick or Select to select from dat1 the elements that have the second array position > than the correspondenting array position of dat1. In this simple explemple, the the element is {45, 9.764}. I tried to use something like that:

Pick[dat1, dat2, dat2[[2]] > dat1[[2]]]

2 Answers

You can use my BoolEval package:

<< BoolEval`

Use an all matrix elements:

BoolPick[dat1, dat1 > dat2]
(* {{12.0541}, {10.789}, {}} *)

Use the second part of each sub-element:

BoolPick[dat1, dat1[[All, 2]] > dat2[[All, 2]]]
(* {{43, 12.0541}, {44, 10.789}} *)

How does it work?

BoolPick[x,y] is just a shorthand for Pick[x, BoolEval[y], 1]. You can see what BoolEval does by evaluating it with symbolic arguments:

BoolEval[a > b]
(* 1 - UnitStep[-a + b] *)

BoolEval is the engine that makes BoolPick so fast.

BoolEval (not BoolPick) is also available as a resource function, but I recommend the package version for slightly better performance and convenience functions such as BoolPick.

Correct answer by Szabolcs on July 11, 2021

Pick[dat1, 1 - UnitStep[dat1[[All, 2]] - dat2[[All, 2]]], 1]
{{45, 9.505}}

Alternatively,

Pick[dat1, Positive[dat2[[All, 2]] - dat1[[All, 2]]]]
{{45, 9.505}}

Pick from dat2 using the same condition:

Pick[dat2, 1 - UnitStep[dat1[[All, 2]] - dat2[[All, 2]]], 1]
{{45, 9.764}}
Pick[dat2, Positive[dat2[[All, 2]] - dat1[[All, 2]]]]
{{45, 9.764}}

Answered by kglr on July 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP