當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Julia Array reshape()用法及代碼示例


這個reshape()是 julia 中的一個內置函數,用於返回與指定數組具有相同數據但具有不同指定維度大小的數組。

用法:
reshape(A, dims)

參數:

  • A:指定數組。
  • dims:指定尺寸。

返回值:它返回一個與指定數組具有相同數據但具有不同指定維度大小的數組。

範例1:




# Julia program to illustrate 
# the use of Array reshape() method
  
# Getting an array with the same data
# as the specified 1D array, but with
# (2 * 2) dimension sizes.
A = [1, 2, 3, 4];
println(reshape(A, (2, 2)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (4 * 4) dimension sizes.
B = [1  2  3  4; 5  6  7  8; 9  10  11  12; 13  14  15  16];
println(reshape(B, (4, 4)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (2 * 8) dimension sizes.
C = [1  5   9  13; 2  6  10  14; 3  7  11  15; 4  8  12  16];
println(reshape(C, 2,:))

輸出:

範例2:


# Julia program to illustrate 
# the use of Array reshape() method
  
# Getting an array with the same data
# as the specified 1D array, but with
# (2 * 2) dimension sizes.
A = ["a", "b", "c", "d"];
println(reshape(A, (2, 2)))
  
# Getting an array with the same data
# as the specified 2D array, but with
# (2 * 2) dimension sizes.
B = ["a" "b"; "c" "d"];
println(reshape(B, (2, 2)))
  
# Getting an array with the same data
# as the specified 3D array, but with
# (2 * 2*3) dimension sizes.
C = cat(["a" "b"; "c" "d"], ["e" "f"; "g" "h"], ["i" "j"; "k" "l"], dims = 3);
println(reshape(C, (2, 2, 3)))

輸出:




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Reshaping array dimensions in Julia | Array reshape() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。