Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia Find Index of Iterative Value

Im trying to find the index of an item in an array within a for loop that steps from item to item in the Array. Is there a built in function that allows me to do this?

dim = 3
length = 10

arrayTuple = fill!(Array(Int64, dim),length)
# [10,10,10]

Arr = fill!(Array(Int64,tuple(arrayTuple...)),1)

for item in Arr
    #print the index of node into array here
end
like image 434
James Beezho Avatar asked Oct 18 '25 18:10

James Beezho


1 Answers

IIUC, you can use enumerate:

julia> for (i, item) in enumerate(Arr[1:5])
           println(i, " ", item)
       end
1 1
2 1
3 1
4 1
5 1

If you want the multidimensional version, you could use eachindex instead:

julia> for i in eachindex(a)
           println(i, " ", a[i])
       end
Base.IteratorsMD.CartesianIndex_3(1,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(2,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(3,1,1) 1.0
Base.IteratorsMD.CartesianIndex_3(1,2,1) 1.0
[... and so on]
like image 168
DSM Avatar answered Oct 20 '25 16:10

DSM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!