How to fill missing values in a loop using conditions in julia?

Viewed 96

I would like to check the loop for number of items and then if the items are of not expected size then fill it with 0's. For example, I have created a loop which tries to access an array's elements for a range of 10

x = range(1, 100, length=45) |> collect
n = trunc(Int, length(x)/10) + 1
s = 1
l = 10
for i in 1:n
    print(x[s:l])
    s += 10
    l +=10
end

In the above code, last iteration doesn't print any result as the number of elements are only 5 but it expects it to be 10. Hence, I would like to know, how may i check in this loop for every iteration the number of elements and if they are not expected then fill it with 0's.

Please suggest and advise on achieving the expected operation.

Thanks!

1 Answers

I think that PaddedViews is what you are looking for:

julia> using PaddedViews

julia> PaddedView(0, x, (ceil(Int, length(x)/10)*10,))
50-element PaddedView(0.0, ::Vector{Float64}, (Base.OneTo(50),)) with eltype Float64:
   1.0
   3.25
   5.5
   7.75
  10.0
  12.25
  14.5
  16.75
   ⋮
  97.75
 100.0
   0.0
   0.0
   0.0
   0.0
   0.0
Related