码迷,mamicode.com
首页 > 其他好文 > 详细

scala sparseVetor, SprseMatrix 实现

时间:2017-09-01 12:46:46      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:for   span   cto   back   size   ble   ret   dom   val   


def rand(seed:Int):Double={

val rand=new Random(seed)
rand.nextDouble()
}



def rand2(size:Int,seed:Int):SparseVector={
val rand=new Random(seed)
val ret_indices=Array.range(0,size)
val ret_values=Array.tabulate[Double](size)(i=>rand.nextDouble()) //从函数创建数组
Vectors.sparse(size, ret_indices, ret_values).asInstanceOf[SparseVector]
}

调用:val w=rand2(4,3)
返回,SparseVector有3个要素函数,size,indices,values.
取某一部分就调用那3个函数。比如:w.size=4,w.indices=(1,2,3,4)




def rand3(numRows:Int,numCols:Int,seed:Int):SparseMatrix={
val rand=new Random(seed)
val ret_rowIndices=new ArrayBuffer[Int]()
for (i <-0 to numCols-1) {
ret_rowIndices ++= Array.range(0,numRows)
}
val ret_colPtrs=Array.range(0,numCols+1).map(x=>x * numRows)
val ret_values=Array.tabulate[Double](numRows*numCols)(i=>rand.nextDouble())
Matrices.sparse(numRows, numCols, ret_colPtrs.toArray,ret_rowIndices.toArray, ret_values.toArray).asInstanceOf[SparseMatrix]
}




scala sparseVetor, SprseMatrix 实现

标签:for   span   cto   back   size   ble   ret   dom   val   

原文地址:http://www.cnblogs.com/zhangbojiangfeng/p/7462632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!