3
$\begingroup$

I have two sets of elements

a={3, 4.8, 5, 4.2, 3.6, 2.3, 4.5, 3.7}

b={2.8, 4.7, 5.3, 3.5, 4.7, 2.8, 4.2, 3.4}

and I create a new set which compares elements of the sets and takes their minimum

c=MapThread[Min, {a, b}]

({c=2.8, 4.7, 5, 3.5, 3.6, 2.3, 4.2, 3.4})

And now I want to find positions of elements in the new set which were changed to smaller elements. So I would like to get something like

{{1},{2},{4},{7},{8}}

Any ideas how could I do it? Thank you for any advices.

$\endgroup$

2 Answers 2

6
$\begingroup$

A few additional alternatives:

Position[0] @ Unitize[b-c]

Position[Alternatives @@ b] @ c

Lookup[PositionIndex @ c, DeleteDuplicates @ b, Nothing]

Map[List] @ Pick[Range @ Length @ c, Unitize[c - b], 0]

all give

{{1}, {2}, {4}, {7}, {8}}

For large input lists, the last one, or a slightly modified version of it

Map[List] @ Pick[Range @ Length @ c,Developer`ToPackedArray @ Unitize[c - b], 0]

is faster than the others.

$\endgroup$
7
$\begingroup$
Position[MapThread[Greater, {a, b}], True]
(*    {{1}, {2}, {4}, {7}, {8}}    *)
$\endgroup$

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.