I was writing Julia code yesterday and came across a bug in my code β the return from findall(f,A) came back with type Array{CartesianIndex{2},1} instead of what I expected, type Array{Int64,1}. Working in the REPL, I always got return type Array{Int64,1} from findall(f,A). Put the same line of code in a function, and findall(f,A) was returning type Array{CartesianIndex{2},1}.
What I discovered is an easily overlooked difference in the return type for findall(f, A) depending on how Array A was defined.
Consider:
function findcart()
βββFindall return type: arrays βββa = Array{Int64}(undef,6) b = Array{Int64}(undef,(6,1)) a[1:6] = [0, 1, 2, 0, -3, 4] b[1:6] = [0, 1, 2, 0, -3, 4] pa = findall(isequal(0),a) pb = findall(isequal(0),b) println("a matrix size: ",size(a),"\tType: ",typeof(a)) println("b matrix size: ",size(b),"\tType: ",typeof(b)) println("Type of pa = ",typeof(pa)) println("Type of pb = ",typeof(pb))
end #findcart
Run this function and the output is:
julia> findcart()
a matrix size: (6,) Type: Array{Int64,1}
b matrix size: (6, 1) Type: Array{Int64,2}
Type of pa = Array{Int64,1}
Type of pb = Array{CartesianIndex{2},1}
Functionally, defining an array as (undef,6) or (undef,(6,1)) are functionally equivalent β you can access a[1], a[1,1], b[1], b[1,1].
I presume the internal indexing is different under the hood, however, between the two different ways that array βaβ and βbβ were defined, and this is what causes the output of findall(f,A) to be different.