2D array filled with consecutive integers

I’m trying to learn my way around array API and I need a 2D array filled with consecutive integers for quick sanity tests. I used this array like object:

w, h = 4, 3
a = reshape(1:w*h, w, h)

but it doesn’t work in cases like this:

julia>   v = view(a, 2, :)
3-element view(reshape(::UnitRange{Int64}, 4, 3), 2, :) with eltype Int64:
  2
  6
 10

julia>   v[1] = 19
ERROR: indexed assignment fails for a reshaped range; consider calling collect

What is a better option?

You might try the suggestion from the error message:

consider calling collect

->

a = reshape(collect(1:w*h), w, h)
2 Likes