I'm trying to implement Matlab's Find function in Julia. In Matlab, the code is
find(A==0)
where A is a very, very large n by m matrix, and where I iterate and update the above over a series of about 500 steps. In Julia, I implement the above via
[findall(x->x==0, D_tot)[j][2] for j in 1:count(x->x==0,D_tot)]
This seems to work nicely, except it goes very slow as I progress with my iteration. For example, for the first step, @time yields
0.000432 seconds (33 allocations: 3.141 KiB)
Step 25:
0.546958 seconds (40.37 k allocations: 389.997 MiB, 7.40% gc time)
Step 65:
1.765892 seconds (86.73 k allocations: 1.516 GiB, 9.63% gc time)
At each step, A remains the same size but becomes more complex, and Julia seems to have trouble finding the zeroes. Is there a better way of implementing Matlab's "Find" function than what I did above?
Going through the Matlab documentation I understand that you want to find
"a vector containing the linear indices of each nonzero element in array X"
and by non-zero you meant true values in Matlab's expression A==0
In that case this can be accomplished as
findall(==(0),vec(D_tot))
And a small benchmark:
D_tot=rand(0:100,1000,1000)
using BenchmarkTools
Running:
julia> @btime findall(==(0), vec($D_tot));
615.100 μs (17 allocations: 256.80 KiB)
julia> @btime findall(iszero, vec($D_tot));
665.799 μs (17 allocations: 256.80 KiB)
That timing is very strange. What's going on with
iszero
? Have you tried several times?yes several times and it is strange indeed. I run
@code_native
and there are tiny differences in the assembly code (even the number of assembly instructions differs by 3) - so this seems not to be random.