分组和转置。我将展示我找出正确顺序的 REPL 会话。
首先,你的数据中有一个分组——你用空格清楚地表明了这一点。每三行,每行内每三个元素。所以我们要做的第一件事就是明确分组。
scala> grid.map(row => (row grouped 3).toArray)
res7: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3), Array(4, 5,
6), Array(7, 8, 9)), Array(Array(11, 12, 13), Array(14, 15, 16), Array(
17, 18, 19)), Array(Array(21, 22, 23), Array(24, 25, 26), Array(27, 28,
29)), Array(Array(31, 32, 33), Array(34, 35, 36), Array(37, 38, 39)), Ar
ray(Array(41, 42, 43), Array(44, 45, 46), Array(47, 48, 49)), Array(Arra
y(51, 52, 53), Array(54, 55, 56), Array(57, 58, 59)), Array(Array(61, 62
, 63), Array(64, 65, 66), Array(67, 68, 69)), Array(Array(71, 72, 73), A
rray(74, 75, 76), Array(77, 78, 79)), Array(Array(81, 82, 83), Array(84,
85, 86), Array(87, 88, 89)))
scala> (res7 grouped 3).toArray
res8: Array[Array[Array[Array[Int]]]] = Array(Array(Array(Array(1, 2, 3)
, Array(4, 5, 6), Array(7, 8, 9)), Array(Array(11, 12, 13), Array(14, 15
, 16), Array(17, 18, 19)), Array(Array(21, 22, 23), Array(24, 25, 26), A
rray(27, 28, 29))), Array(Array(Array(31, 32, 33), Array(34, 35, 36), Ar
ray(37, 38, 39)), Array(Array(41, 42, 43), Array(44, 45, 46), Array(47,
48, 49)), Array(Array(51, 52, 53), Array(54, 55, 56), Array(57, 58, 59))
), Array(Array(Array(61, 62, 63), Array(64, 65, 66), Array(67, 68, 69)),
Array(Array(71, 72, 73), Array(74, 75, 76), Array(77, 78, 79)), Array(A
rray(81, 82, 83), Array(84, 85, 86), Array(87, 88, 89))))
让我们看看我们是否做对了:
scala> res8(0)
res9: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3), Array(4, 5,
6), Array(7, 8, 9)), Array(Array(11, 12, 13), Array(14, 15, 16), Array(
17, 18, 19)), Array(Array(21, 22, 23), Array(24, 25, 26), Array(27, 28,
29)))
是的,这正是我们要转换的数据单位。让我们试一试:
scala> res9.transpose
res10: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3), Array(11,
12, 13), Array(21, 22, 23)), Array(Array(4, 5, 6), Array(14, 15, 16), Ar
ray(24, 25, 26)), Array(Array(7, 8, 9), Array(17, 18, 19), Array(27, 28,
29)))
如预期的!因此,让我们对整个矩阵进行操作:
scala> res8 map (_.transpose)
res11: Array[Array[Array[Array[Int]]]] = Array(Array(Array(Array(1, 2, 3
), Array(11, 12, 13), Array(21, 22, 23)), Array(Array(4, 5, 6), Array(14
, 15, 16), Array(24, 25, 26)), Array(Array(7, 8, 9), Array(17, 18, 19),
Array(27, 28, 29))), Array(Array(Array(31, 32, 33), Array(41, 42, 43), A
rray(51, 52, 53)), Array(Array(34, 35, 36), Array(44, 45, 46), Array(54,
55, 56)), Array(Array(37, 38, 39), Array(47, 48, 49), Array(57, 58, 59)
)), Array(Array(Array(61, 62, 63), Array(71, 72, 73), Array(81, 82, 83))
, Array(Array(64, 65, 66), Array(74, 75, 76), Array(84, 85, 86)), Array(
Array(67, 68, 69), Array(77, 78, 79), Array(87, 88, 89))))
现在我们只需要将其展平即可。我们需要以与分组相反的顺序将其展平:
scala> res11.flatten
res12: Array[Array[Array[Int]]] = Array(Array(Array(1, 2, 3), Array(11,
12, 13), Array(21, 22, 23)), Array(Array(4, 5, 6), Array(14, 15, 16), Ar
ray(24, 25, 26)), Array(Array(7, 8, 9), Array(17, 18, 19), Array(27, 28,
29)), Array(Array(31, 32, 33), Array(41, 42, 43), Array(51, 52, 53)), A
rray(Array(34, 35, 36), Array(44, 45, 46), Array(54, 55, 56)), Array(Arr
ay(37, 38, 39), Array(47, 48, 49), Array(57, 58, 59)), Array(Array(61, 6
2, 63), Array(71, 72, 73), Array(81, 82, 83)), Array(Array(64, 65, 66),
Array(74, 75, 76), Array(84, 85, 86)), Array(Array(67, 68, 69), Array(77
, 78, 79), Array(87, 88, 89)))
scala> res12.map(_.flatten)
res13: Array[Array[Int]] = Array(Array(1, 2, 3, 11, 12, 13, 21, 22, 23),
Array(4, 5, 6, 14, 15, 16, 24, 25, 26), Array(7, 8, 9, 17, 18, 19, 27,
28, 29), Array(31, 32, 33, 41, 42, 43, 51, 52, 53), Array(34, 35, 36, 44
, 45, 46, 54, 55, 56), Array(37, 38, 39, 47, 48, 49, 57, 58, 59), Array(
61, 62, 63, 71, 72, 73, 81, 82, 83), Array(64, 65, 66, 74, 75, 76, 84, 8
5, 86), Array(67, 68, 69, 77, 78, 79, 87, 88, 89))
或者,作为一个班轮:
grid.map(_.grouped(3).toArray).grouped(3).toArray.map(_.transpose).flatten.map(_.flatten)