Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Indices from Mathematica's Select

How can I get the indices of a selection rather than the values. I.e.

list={3->4, 5->2, 1->1, 5->8, 3->2};
Select[list, #[[1]]==5&]; (* returns {5->2, 5->8} *)

I would like something like

SelectIndices[list, #[[1]]==5&]; (* returns {2, 4} *)

EDIT: I found an answer to the immediate question above (see below), but what about sorting. Say I want to sort a list but rather than returning the sorted list, I want to return the indices in the order of the sorted list?

like image 490
John Avatar asked Dec 28 '22 16:12

John


1 Answers

Ok, well, I figured out a way to do this. Mathematica uses such a different vocabulary that searching the documentation still is generally unfruitful for me (I had been searching for things like, "Element index from Mathematica Select", to no avail.)

Anyway, this seems to be the way to do this:

Position[list, 5->_];

I guess its time to read up on patterns in Mathematica.

like image 57
John Avatar answered Jan 02 '23 17:01

John