zvoulgaris

List Slicing drill

Oct 27th, 2020 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.50 KB | None | 0 0
  1. # List slicing
  2.  
  3. function getSlice(x::Array{T, 1}, a::Int64, z::Int64, n::Int64) where T <: Any
  4.     b = a + z - 1;
  5.  
  6.     if b > n
  7.         return vcat(x[a:end], NaN*ones(b-n));
  8.     else
  9.         return x[a:b];
  10.     end
  11. end
  12.  
  13. function main(x::Array{T, 1}, sl::Array{Int64, 1}) where T <: Any
  14.     n = length(x);
  15.     ns = length(sl);
  16.     y = Array{Array{Real, 1}}(undef, ns);
  17.     a = 1;
  18.  
  19.     for i = 1:ns
  20.         y[i] = getSlice(x, a, sl[i], n);
  21.         a += sl[i];
  22.     end
  23.  
  24.     return y;
  25. end
  26.  
Add Comment
Please, Sign In to add comment