amrods
amrods

Reputation: 2131

Reshape "filling" by row

Is there any easy way to reshape a vector into an array in which the "filling" is by row?

More specifically, suppose I have a vector

v = collect(1:8)

reshape "fills" the resulting array by column:

reshape(v, (2,2,2))

2x2x2 Array{Int64,3}:
[:, :, 1] =
 1  3
 2  4

[:, :, 2] =
 5  7
 6  8

But I want to get:

a = Array{Int}(2,2,2)
a[:, :, 1] = [1 2; 3 4]
a[:, :, 2] = [5 6; 7 8]

a
2x2x2 Array{Int64,3}:
[:, :, 1] =
 1  2
 3  4

[:, :, 2] =
 5  6
 7  8

This would be a nice option to add to reshape.

Upvotes: 1

Views: 277

Answers (1)

Felipe Lema
Felipe Lema

Reputation: 2718

mapslices(transpose,reshape(v, (2,2,2)),[1,2])

The keyword you were missing is "transpose". The rest I just took from the docs

Upvotes: 3

Related Questions