Warm tip: This article is reproduced from serverfault.com, please click

Efficiently implementing Matlab's "Find" function in Julia

发布于 2020-12-01 23:59:20

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?

Questioner
Joshuah Heath
Viewed
0
Przemyslaw Szufel 2020-12-02 08:44:03

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)