function - Julia 中带参数的映射函数

标签 function julia apply

我在 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/

相关文章:

python - 使用 python pandas 将具有多行的 python 数据框转换为一行?

python-3.x - 以特定于列的方式填充数字的 nan

c++ - 引用变量的默认值

javascript - 如何在代码隐藏中用 C# 编写 JavaScript 函数?

julia - 函数列表及其在 Julia 中的描述

julia - 如何使用 Julia 从 .CSV 文件数据中读取 DateTime 数据类型

loops - 在 Julia 中存储 For 循环的结果

c - sizeof() 如何通过引用参数传递

javascript - 长字横向溢出时减小字体大小

r - 批量预测;使用 apply() 函数而不是 for 循环。 apply() 函数给出不同点的预测