How to return the index of element in Array in Integer not CartesianIndex?

I have the following array (here is vector), and I want to return the index of “8” element, which should be 4 as integer not CartesianIndex. Any solution?

julia> s=[5 6 7 8]
1×4 Matrix{Int64}:
 5  6  7  8

julia> findall(isequal(8), s)
1-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 4)

It looked like this.

1 Like

In general, you can use LinearIndices to map from cartesian to linear indexing:

s = [5 6 7 8]
inds = findall(isequal(8), s)
li = LinearIndices(s)
li[inds]

Note that the find family of functions return linear indices for input vectors:

v = [5, 6, 7, 8]
findall(isequal(8), v)
3 Likes