如何将SparseVector转换为MatrixEntry

时间:2016-12-19 11:43:54

标签: scala matrix apache-spark

这是我的SparseVector:

mx.foreach(println)

SparseVector((0,1.0), (1,0.0), (2,0.0), (3,0.0), (4,0.0), (5,0.0), (6,0.0), (7,0.0), (8,0.0), (9,0.0), (10,0.0), (11,0.0), (12,0.0))
SparseVector((0,0.0), (1,1.0), (2,0.0), (3,0.0), (4,0.0), (5,0.0), (6,0.0), (7,0.0), (8,0.0), (9,0.0), (10,0.0), (11,0.0), (12,0.0))
SparseVector((0,0.0), (1,0.0), (2,1.0), (3,0.0), (4,0.0), (5,0.0), (6,0.0), (7,0.0), (8,0.0), (9,0.0), (10,0.0), (11,0.0), (12,0.0))
SparseVector((0,0.0), (1,0.0), (2,0.0), (3,1.0), (4,0.0), (5,0.0), (6,0.0), (7,0.0), (8,0.0), (9,0.0), (10,0.0), (11,0.0), (12,0.0))

我想转换为MatrixEntry?

1 个答案:

答案 0 :(得分:1)

编辑:鉴于mx的类型为RDD[SparseVector[Double]]

val matrixEntries: RDD[MatrixEntry] = mx.zipWithIndex.flatMap {
  case (vector, i) => vector.toArray.zipWithIndex.map {
    case (v, j) => MatrixEntry(i, j, v)
  }
}