朱莉娅:如何在朱莉娅中随机排列一个向量? [英] JULIA : How to permute randomly a vector in julia?
问题描述
我有一个随机数向量,我想使用randperm()函数随机置换如下,但它不起作用.
l have a vector of random numbers that l want to permute randomly using randperm() function as follows but it's not working.
X=rand(100000) # a vector of 100000 random elements
Y=randperm(X) # want to permute randomly the vector x
返回的错误是: 错误:MethodError:没有与randperm(:: Array {Float64,1})匹配的方法 在./boot.jl:237的eval(:: Module,:: Any)中
the returned error is : ERROR: MethodError: no method matching randperm(::Array{Float64,1}) in eval(::Module, ::Any) at ./boot.jl:237
谢谢
推荐答案
基于文档 randperm()
接受整数n
并给出长度为n的排列.您可以使用以下顺序对原始向量进行重新排序:
Based on the docs the randperm()
accepts an integer n
and gives a permutation of length n. You can use this ordering to then reorder your original vector:
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
您始终可以通过在REPL中键入?function_name
来检查文档.
You can always check the docs by typing ?function_name
in the REPL.
如果您的唯一目标是随机置换向量,则也可以使用shuffle()
:
If your only goal is to randomly permute the vector, you can also use shuffle()
:
julia> shuffle(X)
5-element Array{Int64,1}:
5
4
1
2
3
这篇关于朱莉娅:如何在朱莉娅中随机排列一个向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!