matlab - 在 Julia 中高效实现 Matlab 的“查找”功能
问题描述
我正在尝试在 Julia 中实现 Matlab 的 Find 功能。在 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 的“查找”功能?
解决方案
浏览 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)
推荐阅读
- javascript - JavaScript 承诺?
- c++ - 如何检测 Windows 更新状态
- python - 如何通过对某些术语的附加权重来实施 TF-IDF 评分
- python - 从 Tensorflow 转换 -> CoreML 3.0 用于槽/意图检测
- android - 无法使用 Firebase 创建帐户?
- laravel - Laravel 中的 HTML DOMParser
- elasticsearch - 不同的高级 Rest 客户端弹性查询
- asp.net - 如何通过端口访问具有虚拟路径的文件?
- python - 如何手动将颜色分配给分类变量的类型?
- java - Alluxio master动态输出verbose正常吗