Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FInd location of element in a vector

I'm new to APL and I would like to find the position of an element(s) within a vector. For example, if I create a vector of 50 random numbers:

lst ←  50 ? 100

How can I find the positions of 91 assuming it occurs 3 times in the vector?

Thanks.

like image 401
awyr_agored Avatar asked Dec 18 '25 23:12

awyr_agored


2 Answers

I'm not an expert, but a simple way is to just select the elements from ⍳ 100 where the corresponding element in lst is 91

(lst=91)/⍳100
like image 83
Probie Avatar answered Dec 21 '25 01:12

Probie


With Dyalog 16.0, you can use the new monadic function "Where".

⍸lst=91

lst=91 gives a vector of 0s and 1s. Applying on this gives the locations of all the 1s. This also works if lst is a matrix.

like image 44
user41805 Avatar answered Dec 21 '25 02:12

user41805