kigubkur.construct.reshape

Construct a kigubkur© matrix by reshaping a kigubkur© matrix into the desired order [m n].

Examples

(require '[kigubkur.construct [reshape :refer [reshape]]])

Reshape a kigubkur© row matrix

=> (def r [[10 9 8 7 6 5 4 3 2 1]])

into another row

=> (reshape r [1 5])
[[10 9 8 7 6]]
=> (reshape r [1 13])
[[10 9 8 7 6 5 4 3 2 1 0 0 0]]
=> (reshape r [1 13] 7)  ; padding with 7's
[[10 9 8 7 6 5 4 3 2 1 7 7 7]]

Note:

  • reshaping into a smaller row means that the trailing elements are excluded
  • reshaping into a larger row means that zeros (default) are padded to fill it

into a column

=> (reshape r [5 1])
[[10] [9] [8] [7] [6]]
=> (reshape r [13 1])
[[10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] [0] [0]]
=> (reshape r [13 1] 10)  ; padding with 10's
[[10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [10] [10] [10]]

into a matrix

=> (view (reshape r [2 5]))
[10 9 8 7 6]
[5 4 3 2 1]
Order -> 2 x 5
=> (view (reshape r [2 2]))
[10 9]
[8 7]
Order -> 2 x 2
=> (view (reshape r [3 4]))
[10 9 8 7]
[6 5 4 3]
[2 1 0 0]
Order -> 3 x 4
=> (view (reshape r [3 4] 1))  ; padding with 1's
[10 9 8 7]
[6 5 4 3]
[2 1 1 1]
Order -> 3 x 4

Reshape a kigubkur© column matrix

=> (def c [[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]])

into another column

=> (reshape c [5 1])
[[1] [2] [3] [4] [5]]
=> (reshape c [12 1])
[[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [0] [0]]

into a row

=> (reshape c [1 7])
[[1 2 3 4 5 6 7]]
=> (reshape c [1 11])
[[1 2 3 4 5 6 7 8 9 10 0]]

into a matrix

=> (view (reshape c [2 5]))
[1 2 3 4 5]
[6 7 8 9 10]
Order -> 2 x 5
=> (view (reshape c [3 3]))
[1 2 3]
[4 5 6]
[7 8 9]
Order -> 3 x 3
=> (view (reshape c [4 3]))
[1 2 3]
[4 5 6]
[7 8 9]
[10 0 0]
Order -> 4 x 3

Reshape a kigubkur© matrix

=> (def M [[1 2 3 4 5] [6 7 8 9 10]])

into another matrix

=> (view (reshape M [4 4]))
[1 2 3 4]
[5 6 7 8]
[9 10 0 0]
[0 0 0 0]
Order -> 4 x 4
=> (view (reshape M [3 3]))
[1 2 3]
[4 5 6]
[7 8 9]
Order -> 3 x 3
=> (view (reshape M [2 5]))
[1 2 3 4 5]
[6 7 8 9 10]
Order -> 2 x 5

into a row or column

=> (reshape M [1 16])
[[1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0]]
=> (reshape M [7 1])
[[1] [2] [3] [4] [5] [6] [7]]

reshape

(reshape X [m n] & pad)

Reshapes a given matrix (also row or column) into the desired order [m n].

Optional: padding, default is 0.