Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: merge two arrays in one single structure

Tags:

arrays

scala

I have two arrays:

val diceProbDist = new Array[Double](2 * DICE + 1)

and

val diceExpDist = new Array[Double](2 * DICE + 1)

and I want to merge in one single structure (some sort of tuple, maybe):

(0, 0.0, 0,0)(1, 0.0, 0.0)(2, 0.02778, 0.02878)...

where the first entry is the array index, the second entry is the first array value and the third entry is the second array value.

Is there some scala function to accomplish that (zip with map or something like that)?

thanks, ML

like image 986
MLeiria Avatar asked Dec 11 '22 13:12

MLeiria


1 Answers

val diceProbDist = Array(0.1, 0.2, 0.3)
val diceExpDist = Array(0.11, 0.22, 0.33)

diceProbDist
 .zip(diceExpDist)
 .zipWithIndex
 .map { case ((v1, v2), i) => (i, v1, v2) }

// result: Array((0,0.1,0.11), (1,0.2,0.22), (2,0.3,0.33))
like image 153
Tzach Zohar Avatar answered Dec 27 '22 14:12

Tzach Zohar