如何合并两个二维数组

huangapple go评论70阅读模式
英文:

How to union two 2D arrays

问题

Kotlin或Java标准库中是否有内置函数可以“合并”两个2D数组?通过合并,我指的是以下内容:

val a = arrayOf(
    arrayOf(1, 0),
    arrayOf(0, 0)
)

val b = arrayOf(
    arrayOf(1, 0),
    arrayOf(0, 1)
)

val result = a.union(b) { da, db -> da + db // 决定如何合并值 }
val shouldBe = arrayOf(
    arrayOf(2, 0),
    arrayOf(0, 1)
)

assert(result == shouldBe)

结果数组的形状应该与调用union的数组相同。

我可以自己编写这个功能,但想知道是否已经存在?

英文:

Is there a built in function in the Kotlin or Java standard library to "union" two 2D arrays? By union, I mean the following:

val a = arrayOf(
    arrayOf(1, 0),
    arrayOf(0, 0)
)

val b = arrayOf(
    arrayOf(1, 0),
    arrayOf(0, 1)
)

val result = a.union(b) { da, db -> da + db // Decide how to merge the values }
val shouldBe = arrayOf(
    arrayOf(2, 0),
    arrayOf(0, 1)
)

assert(result == shouldBe)

The shape of the resulting array should be that of the array which union is called on.

I can obviously write this myself, but wondering if it already exists?

答案1

得分: 2

答案是否定的,没有内置的方法。对于Kotlin,您可以查看JetBrains开发的这个库:viktor。我没有使用过它,只是在第一个版本发布时玩弄过(所以我不能真正说什么)。

如果您只有包含2D整数矩阵的简单情况,那么您可以尝试以下方法:

fun Array<Array<Int>>.matrixSum(b: Array<Array<Int>>): Array<Array<Int>> {
  return mapIndexed { indexA, valueA ->
    valueA.mapIndexed { indexB, valueB ->
      valueB + b[indexA][indexB]
    }.toTypedArray()
  }.toTypedArray()
}

val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))

val result = a.matrixSum(b)

或者您可以将操作作为参数传递给matrixOp函数:

fun Array<Array<Int>>.matrixOp(b: Array<Array<Int>>, op: (Int, Int) -> Int): Array<Array<Int>> {
  return mapIndexed { indexA, valueA ->
    valueA.mapIndexed { indexB, valueB ->
      op(valueB, b[indexA][indexB])
    }.toTypedArray()
  }.toTypedArray()
}

val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))

val summed = a.matrixOp(b) { i1, i2 -> i1 + i2 }
val multiplied = a.matrixOp(b) { i1, i2 -> i1 * i2 }
英文:

The answer is no, there is nothing built-in. For Kotlin you could have a look at this library by JetBrains: viktor. I've not used it, just toyed around with it when the first version was published (so I can't really say anything about it).

If you have only simple cases with 2D-Int-matrices, then you could try something along this:

fun Array&lt;Array&lt;Int&gt;&gt;.matrixSum(b: Array&lt;Array&lt;Int&gt;&gt;): Array&lt;Array&lt;Int&gt;&gt; {
  return mapIndexed { indexA, valueA -&gt;
    valueA.mapIndexed { indexB, valueB -&gt;
      valueB + b[indexA][indexB]
    }.toTypedArray()
  }.toTypedArray()
}

val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))

val result = a.matrixSum(b)

Or you hand over the operation to apply as argument:

fun Array&lt;Array&lt;Int&gt;&gt;.matrixOp(b: Array&lt;Array&lt;Int&gt;&gt;, op: (Int, Int) -&gt; Int): Array&lt;Array&lt;Int&gt;&gt; {
  return mapIndexed { indexA, valueA -&gt;
    valueA.mapIndexed { indexB, valueB -&gt;
      op(valueB, b[indexA][indexB])
    }.toTypedArray()
  }.toTypedArray()
}

val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))

val summed = a.matrixOp(b) { i1, i2 -&gt; i1 + i2 }
val multiplied = a.matrixOp(b) { i1, i2 -&gt; i1 * i2 }

huangapple
  • 本文由 发表于 2023年5月28日 15:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定