簡體   English   中英

在Julia中高效實現Matlab的“查找”function

[英]Efficiently implementing Matlab's "Find" function in Julia

我正在嘗試在 Julia 中實現 Matlab 的查找 function。在 Matlab 中,代碼是

find(A==0)

其中 A 是一個非常非常大的 n x m 矩陣,我在大約 500 步的一系列過程中迭代和更新上述內容。 在 Julia 中,我通過

[findall(x->x==0, D_tot)[j][2] for j in 1:count(x->x==0,D_tot)]

這似乎工作得很好,除了隨着我的迭代進展它變得非常慢。 例如,對於第一步,@time 產生

0.000432 seconds (33 allocations: 3.141 KiB)

第 25 步:

0.546958 seconds (40.37 k allocations: 389.997 MiB, 7.40% gc time)

步驟 65:

1.765892 seconds (86.73 k allocations: 1.516 GiB, 9.63% gc time)

在每一步中,A 的大小都保持不變,但變得更加復雜,Julia 似乎很難找到零點。 有沒有比我上面做的更好的方法來實現 Matlab 的“查找”function?

通過 Matlab 文檔我知道你想找到

“包含數組 X 中每個非零元素的線性索引的向量”

非零是指 Matlab 表達式A==0中的真值

在那種情況下,這可以完成為

findall(==(0),vec(D_tot))

還有一個小基准:

D_tot=rand(0:100,1000,1000)
using BenchmarkTools

跑步:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM