我在 Julia 中创建了以下函数:
using StatsBase
function samplesmallGram(A::AbstractMatrix)
n=size(A,1)
kpoints=sort(sample((1:n),Int(0.05*n),replace=false))
Lsmall=A[kpoints,kpoints]
return kpoints,Lsmall
end
我想将此函数应用于方形对称矩阵 10 次
L
我有,通过 map()
命令,而不是 for
环形。我试过map(samplesmallGram(L), 1:1:10)
但它不起作用......我怎样才能做到这一点?
最佳答案
通常 map 用于集合的每个元素,就像每个元素的转换过程。
https://docs.julialang.org/en/v1/base/collections/index.html#Base.map
julia> map(x -> x * 2, [1, 2, 3])
3-element Array{Int64,1}:
2
4
6
julia> map(+, [1, 2, 3], [10, 20, 30])
3-element Array{Int64,1}:
11
22
33
还要看看reducers的想法。它们是相关的。您可以将 L 作为全局变量传入,也可以在调用时使用箭头符号。
箭头符号
output = map(x -> samplesmallGram(L), 1:1:10)
请注意,在这种情况下 x 不是函数的参数,而是L 被通过了 10 次。
全局的
A = []
function samplesmallGram(index)
global A
n=size(A,1)
kpoints=sort(sample((1:n),Int(0.05*n),replace=false))
Lsmall=A[kpoints,kpoints]
return kpoints,Lsmall
end
output = map(samplesmallGram, 1:1:10)
希望有帮助。
关于function - Julia 中带参数的映射函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260592/