朱莉娅:如何在朱莉娅中随机置换一个载体?

vin*_*cet 2 julia permute

我有一个随机数的向量,我想使用randperm()函数随机置换,如下所示,但它不起作用.

X=rand(100000) # a vector of 100000 random elements
Y=randperm(X) # want to permute randomly the vector x
Run Code Online (Sandbox Code Playgroud)

返回的错误是: 错误:MethodError:没有方法匹配./boot.jl:237中的eval(:: Module,:: Any)中的randperm(:: Array {Float64,1})

谢谢

nic*_*y12 8

基于文档,randperm()接受一个整数n并给出长度为n的排列.您可以使用此顺序然后重新排序原始矢量:

julia> X = collect(1:5)
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> Y = X[randperm(length(X))]
5-element Array{Int64,1}:
 3
 4
 1
 2
 5
Run Code Online (Sandbox Code Playgroud)

您可以随时通过键入?function_nameREPL 来检查文档.

如果您的唯一目标是随机置换矢量,您还可以使用shuffle():

julia> shuffle(X)
5-element Array{Int64,1}:
 5
 4
 1
 2
 3
Run Code Online (Sandbox Code Playgroud)