4
$\begingroup$

Consider

r = Array[Total[{##}] &, {3, 3}]
(*{{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}*)
pos = {{1, 1}, {2, 2}};

This

r[[pos]]

gives the err

Part::pkspec1: The expression {{1,1},{2,2}} cannot be used as a part specification.

instead of the expected

{2,4}

In other words how to pick elements of a list at specific positions?

$\endgroup$

1 Answer 1

5
$\begingroup$

You can Apply (@@@) Part on pos:

r[[##]] & @@@ pos
 {2, 4}

You can also use Extract or Indexed or Query:

Extract[pos] @ r
 {2, 4}
Indexed[r, #] & /@ pos
 {2, 4}
Query[##] @ r & @@@ pos
 {2, 4}
$\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.