logo

ジュリアで配列を平行移動する方法 📂ジュリア

ジュリアで配列を平行移動する方法

説明

circshifr(A, (n,m))を使用すると、配列Aの行を$n$カン下にシフトさせ、列を$m$カン右にシフトさせることができる。(n,m)は整数から成るタプルでなければならず、もちろん負の数も可能だ。負の数の場合は逆方向に平行移動される。

3次元以上の配列の場合は、一番小さい2次元配列にそれぞれ適用される。

コード

2次元配列

julia> A = transpose(reshape(1:25,5,5))
5×5 LinearAlgebra.Transpose{Int64,Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}}:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20
 21  22  23  24  25

julia> circshift(A, (-1,0))
5×5 Array{Int64,2}:
  6   7   8   9  10
 11  12  13  14  15
 16  17  18  19  20
 21  22  23  24  25
  1   2   3   4   5

julia> circshift(A, (0,3))
5×5 Array{Int64,2}:
  3   4   5   1   2
  8   9  10   6   7
 13  14  15  11  12
 18  19  20  16  17
 23  24  25  21  22

julia> circshift(A, (-1,3))
5×5 Array{Int64,2}:
  8   9  10   6   7
 13  14  15  11  12
 18  19  20  16  17
 23  24  25  21  22
  3   4   5   1   2

高次元配列

julia> B = reshape(1:4*4*3,4,4,3)
4×4×3 reshape(::UnitRange{Int64}, 4, 4, 3) with eltype Int64:
[:, :, 1] =
 1  5   9  13
 2  6  10  14
 3  7  11  15
 4  8  12  16

[:, :, 2] =
 17  21  25  29
 18  22  26  30
 19  23  27  31
 20  24  28  32

[:, :, 3] =
 33  37  41  45
 34  38  42  46
 35  39  43  47
 36  40  44  48

julia> circshift(B,(-1,0))
4×4×3 Array{Int64,3}:
[:, :, 1] =
 2  6  10  14
 3  7  11  15
 4  8  12  16
 1  5   9  13

[:, :, 2] =
 18  22  26  30
 19  23  27  31
 20  24  28  32
 17  21  25  29

[:, :, 3] =
 34  38  42  46
 35  39  43  47
 36  40  44  48
 33  37  41  45

julia> circshift(B,(0,2))
4×4×3 Array{Int64,3}:
[:, :, 1] =
  9  13  1  5
 10  14  2  6
 11  15  3  7
 12  16  4  8

[:, :, 2] =
 25  29  17  21
 26  30  18  22
 27  31  19  23
 28  32  20  24

[:, :, 3] =
 41  45  33  37
 42  46  34  38
 43  47  35  39
 44  48  36  40

julia> circshift(B,(-1,2))
4×4×3 Array{Int64,3}:
[:, :, 1] =
 10  14  2  6
 11  15  3  7
 12  16  4  8
  9  13  1  5

[:, :, 2] =
 26  30  18  22
 27  31  19  23
 28  32  20  24
 25  29  17  21

[:, :, 3] =
 42  46  34  38
 43  47  35  39
 44  48  36  40
 41  45  33  37
 julia> C = reshape(1:3*3*2*4,3,3,2,4)
3×3×2×4 reshape(::UnitRange{Int64}, 3, 3, 2, 4) with eltype Int64:
[:, :, 1, 1] =
 1  4  7
 2  5  8
 3  6  9

[:, :, 2, 1] =
 10  13  16
 11  14  17
 12  15  18

[:, :, 1, 2] =
 19  22  25
 20  23  26
 21  24  27

[:, :, 2, 2] =
 28  31  34
 29  32  35
 30  33  36

[:, :, 1, 3] =
 37  40  43
 38  41  44
 39  42  45

[:, :, 2, 3] =
 46  49  52
 47  50  53
 48  51  54

[:, :, 1, 4] =
 55  58  61
 56  59  62
 57  60  63

[:, :, 2, 4] =
 64  67  70
 65  68  71
 66  69  72

julia> circshift(C,(1,1))
3×3×2×4 Array{Int64,4}:
[:, :, 1, 1] =
 9  3  6
 7  1  4
 8  2  5

[:, :, 2, 1] =
 18  12  15
 16  10  13
 17  11  14

[:, :, 1, 2] =
 27  21  24
 25  19  22
 26  20  23

[:, :, 2, 2] =
 36  30  33
 34  28  31
 35  29  32

[:, :, 1, 3] =
 45  39  42
 43  37  40
 44  38  41

[:, :, 2, 3] =
 54  48  51
 52  46  49
 53  47  50

[:, :, 1, 4] =
 63  57  60
 61  55  58
 62  56  59

[:, :, 2, 4] =
 72  66  69
 70  64  67
 71  65  68

環境

  • OS: Windows10
  • バージョン: 1.5.3 (2020-11-09)