Reshape in place

I’m trying to reshape in place using:

using Random
Random.seed!(1)

A = rand(Int8, 4)
show(A)

reshape(A, (2, 2))

println("\n")
show(A)
println("\n")

Which should change the algorithm which maps the array into a matrix one, at least following reshape » Julia Functions and Convert array into matrix in place - #4 by anon74562486

However it prints the same object.

If I instead write:

A = reshape(A, (2, 2))

It works, but I don’t know if it just allocates a different A, discarding the previous one

reshape makes a view of the same underlying data.

julia> A = rand(Int, 4)
4-element Vector{Int64}:
   176552731437150339
 -6026423638257492371
  2097435220302348986
  8772925418816777814

julia> B = reshape(A, 2,2)
2×2 Matrix{Int64}:
   176552731437150339  2097435220302348986
 -6026423638257492371  8772925418816777814

julia> B[:] .= 0;

julia> B
2×2 Matrix{Int64}:
 0  0
 0  0

julia> A
4-element Vector{Int64}:
 0
 0
 0
 0
1 Like