# Use of getindex.(L,1) in a for loop

I am converting some old Julia 0.5 code into Matlab. I know broadcasting uses the dot notation but I don’t understand why you would want to use:

L = getindex.(L,1)

inside a for or while loop in Julia code. At the command prompt you get the same as the variable name as output so in Matlab I am just ignoring the getindex part so:

g = getindex.(function(blah),1)

I just say is:

g = function(blah)

which seems to work fine but I must be missing the point of what the original code was trying to do.
thanks,

`getindex` does what square brackets does.

``````julia> a = rand(2,2)
2×2 Array{Float64,2}:
0.573604  0.332509
0.571927  0.137868

julia> a[1]
0.5736038727235953

julia> getindex(a, 1)
0.5736038727235953

julia> a[1, :]
2-element Array{Float64,1}:
0.5736038727235953
0.332508603490159

julia> getindex(a, 1, :)
2-element Array{Float64,1}:
0.5736038727235953
0.332508603490159
``````

`getindex.(L, 1)` is broadcasting the getindex operation on each element of `L`. In this case, assuming the elements of `L` don’t have custom indexing, it’s the same as doing `first.(L)`. Note that L = `getindex.(L, 1)` is very probably not type stable.

1 Like

I think it must have been to remove cells or square brackets around values within an array. So I guess that is why you said it is not type stable. I found you can remove square brackets in an array. I realise this doesn’t make much sense.

eg:

L = [1,2,[3]]
then use:

L = getindex.(L,1)

recovers the array as floating point numbers.

I might be wrong in assuming that was why it was used.