重塑数组,求教


#1

Julia是否有类似于MATLAB中reshape函数一样的命令,能够给数组变形。
我目前想把一个一维的数组变成一个四阶张量,想找一个类似reshape的功能。
感谢并期待您的帮助。


#2

为什么不先试一下呢?

help?> reshape
search: reshape promote_shape

  reshape(A, dims...) -> AbstractArray
  reshape(A, dims) -> AbstractArray

  Return an array with the same data as A, but with different dimension sizes or number of dimensions. The two arrays
  share the same underlying data, so that the result is mutable if and only if A is mutable, and setting elements of
  one alters the values of the other.

  The new dimensions may be specified either as a list of arguments or as a shape tuple. At most one dimension may be
  specified with a :, in which case its length is computed such that its product with all the specified dimensions is
  equal to the length of the original array A. The total number of elements must not change.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> A = Vector(1:16)
  16-element Array{Int64,1}:
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16

  julia> reshape(A, (4, 4))
  4×4 Array{Int64,2}:
   1  5   9  13
   2  6  10  14
   3  7  11  15
   4  8  12  16

  julia> reshape(A, 2, :)
  2×8 Array{Int64,2}:
   1  3  5  7   9  11  13  15
   2  4  6  8  10  12  14  16

  julia> reshape(1:6, 2, 3)
  2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
   1  3  5
   2  4  6

#3

发完帖子就在帮助文档里找到了reshape函数:joy:
非常感谢指导!