Function to split a vector into n equal sized vectors

2,602 views
Skip to first unread message

josha W

unread,
May 21, 2015, 5:54:39 PM5/21/15
to julia...@googlegroups.com
R has a nice function 'split' can do this job. See the example below as reference.

  train_all =as.matrix(sample (1:254, 254))
  train_5_group<-split (train_all, rep (1:5), drop=true)

Is there a similar function in julia?


Alex

unread,
May 22, 2015, 3:32:20 AM5/22/15
to julia...@googlegroups.com
Hi Josha,

I am not sure if there is a function like that in the standard library. But you can use something like this

function rsplit!(v,r)
        a
= Array(Vector{eltype(v)},0)
       
while length(v) >= length(r)
            push
!(a, splice!(v,r))
       
end
       
!isempty(v) && push!(a, v)
   
return a
end

and use it like that
julia> v = rand(10)
10-element Array{Float64,1}:
 
0.471785
 
0.305048
 
0.407115
 
0.696377
 
0.157388
 
0.809052
 
0.282674
 
0.885787
 
0.0478649
 
0.268095

julia
> rsplit!(v,1:3)
4-element Array{Array{Float64,1},1}:
 
[0.471785,0.305048,0.407115]
 
[0.696377,0.157388,0.809052]
 
[0.282674,0.885787,0.0478649]
 
[0.268095]

(If you don't want to have the last entry remove the  !isempty(v)... line or introduce a keyword to switch it on and off). Note that v is modified by rsplit!

Alternatively you can reshape your vector and extract columns:
function rsplit( v, l::Int)
    m
= reshape(v,l,div(length(v),l))
   
return [m[:,i] for i=1:size(m,2)]
end

(Here you have to make sure that the length of v is a multiple of l)

Hope this helps,

Alex.

josha W

unread,
May 22, 2015, 1:07:34 PM5/22/15
to julia...@googlegroups.com
Hi, Alex,

Thank you very much for the coding. It is really helpful. However, this code itself can not solve all the problem because since there are two additional requirements for this task (sorry I forgot to mention on the first place) . 

1) the sampling have to be random
2) the last few entries (say b) have to be evenly split into the first b vectors.

so. based on your code I created my first Julia function and solved the problem. 

function k_fold!(nObs,k)
  n=sort(sample(1:nObs,nObs,replace=false))

  b=nObs % k
  a=(nObs-b)/k
  a=convert(Int16,a)
  k_fold = Array(Vector{eltype(n)},0)

  for i in 1:b
    m=sample(n, a+1,replace=false)
    push!(k_fold, m)
    n=setdiff(n,m)
  end

  for j in 1:k-b
    m=sample(n, a,replace=false)
    push!(k_fold, m)
    n=setdiff(n,m)
  end
  return k_fold
end   


Thanks again and welcome any further modification to trim this bulky code.  
Reply all
Reply to author
Forward
0 new messages