Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Scala中展开平面阵列_Arrays_Scala_Recursion_Multidimensional Array - Fatal编程技术网

Arrays 在Scala中展开平面阵列

Arrays 在Scala中展开平面阵列,arrays,scala,recursion,multidimensional-array,Arrays,Scala,Recursion,Multidimensional Array,我有一个这样的平面阵列和另一个描述维度的平面阵列: val elems = Array(0,1,2,3) val dimensions = Array(2,2) 所以现在我必须能够解开它并返回一个2*2数组,如下所示: val unflattened = {{0,1},{2,3}} 尺寸可以是任意顺序。唯一的条件是平面阵列的长度等于尺寸的乘积。例如,如果尺寸是 数组(3,3) 那么我希望elems平面阵列中必须有9个元素!先决条件将在其他地方检查,因此我不必在这里担心它!我所需要做的就是返回

我有一个这样的平面阵列和另一个描述维度的平面阵列:

val elems = Array(0,1,2,3)
val dimensions = Array(2,2)
所以现在我必须能够解开它并返回一个2*2数组,如下所示:

val unflattened = {{0,1},{2,3}}
尺寸可以是任意顺序。唯一的条件是平面阵列的长度等于尺寸的乘积。例如,如果尺寸是

数组(3,3)

那么我希望elems平面阵列中必须有9个元素!先决条件将在其他地方检查,因此我不必在这里担心它!我所需要做的就是返回一个未格式化的数组

由于这必须适用于任何维度大小,我想我可能必须定义一个递归结构来放置我的结果!像这样的

case class Elem(elem: Array[Elem])
这行吗


关于如何实现该函数的任何线索?

在数组上有一个函数
分组
,它可以满足您的需要

@数组(0,1,2,3).分组(2).到数组
res2:Array[Array[Int]=Array(Array(0,1),Array(2,3))
这里有一个解决方案:

def unflatten(flat: Vector[Any], dims: Vector[Int]): Vector[Any] =
  if (dims.length <= 1) {
    flat
  } else {
    val (Vector(dim), rest) = dims.splitAt(1)

    flat.grouped(flat.length/dim).map(a => unflatten(a, rest)).toVector
  }

虽然您应该能够使用一个简单的递归结构来实现这一点,但我还是选择了一个更适合这个问题的结构

case class Row(elems: List[Int])

trait Matrix
case class SimpleMatrix(rows: List[Row]) extends Matrix
case class HigherMatrix(matrices: List[Matrix]) extends Matrix

// since your flat arrays are always of proper sizes... we are not handling error cases
// so we are dealing with higher N-dimension matrices with size List(s1, s2, ...,sN)
// I have chosen List for the example (as its easy to print), you should choose Array

def arrayToMatrix(flat: List[Int], dimension: Int, sizes: List[Int]): Matrix = dimension match {
  case 1 | 2 =>
    // since your flat arrays are always of proper sizes... there should not be any problems here
    SimpleMatrix(
      flat
        .grouped(sizes.head)
        .map(Row)
        .toList
    )
  case _ =>
    HigherMatrix(
      flat
        .grouped(sizes.tail.reduce(_ * _))
        .map(g => arrayToMatrix(g, dimension - 1, sizes.tail))
        .toList
    )
}

def arrayToSquareMatrix(flat: List[Int], dimension: Int, size: Int): Matrix =
  arrayToMatrix(flat, dimension, Range.inclusive(1, dimension).map(_ => size).toList)
下面是一些例子

val sm_2__2_2 = arrayToSquareMatrix(Range.inclusive(1, 4).toList, 2, 2)
// sm_2__2_2: Matrix = SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4))))

val m_2__3_2 = arrayToMatrix(Range.inclusive(1, 6).toList, 2, List(3, 2))
// m_2__3_2: Matrix = SimpleMatrix(List(Row(List(1, 2, 3)), Row(List(4, 5, 6))))

val sm_3__2_2_2 = arrayToSquareMatrix(Range.inclusive(1, 8).toList, 3, 2)
// sm_3__2_2_2: Matrix = HigherMatrix(List(SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4)))), SimpleMatrix(List(Row(List(5, 6)), Row(List(7, 8))))))

val m_3__3_2_2 = arrayToMatrix(Range.inclusive(1, 12).toList, 3, List(3, 2, 2))
// m_3__3_2_2: Matrix = HigherMatrix(List(SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4)))), SimpleMatrix(List(Row(List(5, 6)), Row(List(7, 8)))), SimpleMatrix(List(Row(List(9, 10)), Row(List(11, 12))))))

您需要考虑如何使用此操作的结果。这将有助于澄清您想要的结果类型,进而有助于澄清获得该结果所需的算法。这些结果稍后将由另一个程序解释。此函数只需获取这两个参数,并将结果作为适当的维度返回!那么,由另一个程序解释的“结果”的格式是什么呢?“适当的维度”在数据文件中是如何表达的?我明白你的意思。我可以返回一个对象或一个任意类型的函数,由调用方来获取合适的类型。我还可以传回一些信息,比如返回类型是数组,如果是,有多少维度!您可以有多个二维数组吗?但这对任何数量的维度都不起作用,不是吗?问题很不清楚,但想法是允许每个维度有不同的大小,而不是所有维度都有一个大小。@Tim即使这是必需的。。。我们可以很容易地调整它以匹配它。@Tim更新为包括其他维度中的任何大小。val sm_3_2是什么?它的维度是什么?对我来说,它是一个3x2矩阵,这意味着它在平面结构中总共应该有6个元素,但是你的样本有8个元素。因此,它应该生成一个包含3行2列的矩阵,但结果是4行2列。这是不对的
sm_3__2
是一个正方形(更精确地说是立方体)
3维
矩阵,大小为
2*2*2
,因此它将有
8个
元素。而
m_3_3_2_2
是一个非正方形
3维
矩阵,大小
3*2*2
,因此它有
12个
元素。是的,有一个问题,正在解决。
val sm_2__2_2 = arrayToSquareMatrix(Range.inclusive(1, 4).toList, 2, 2)
// sm_2__2_2: Matrix = SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4))))

val m_2__3_2 = arrayToMatrix(Range.inclusive(1, 6).toList, 2, List(3, 2))
// m_2__3_2: Matrix = SimpleMatrix(List(Row(List(1, 2, 3)), Row(List(4, 5, 6))))

val sm_3__2_2_2 = arrayToSquareMatrix(Range.inclusive(1, 8).toList, 3, 2)
// sm_3__2_2_2: Matrix = HigherMatrix(List(SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4)))), SimpleMatrix(List(Row(List(5, 6)), Row(List(7, 8))))))

val m_3__3_2_2 = arrayToMatrix(Range.inclusive(1, 12).toList, 3, List(3, 2, 2))
// m_3__3_2_2: Matrix = HigherMatrix(List(SimpleMatrix(List(Row(List(1, 2)), Row(List(3, 4)))), SimpleMatrix(List(Row(List(5, 6)), Row(List(7, 8)))), SimpleMatrix(List(Row(List(9, 10)), Row(List(11, 12))))))