Monday, 15 April 2013

scalala - Read a matrix from a file in Scala Breeze -



scalala - Read a matrix from a file in Scala Breeze -

i want read tab-delimited text file breeze densematrix. see in scaladoc should possible , there whole set of i/o classes, can't find examples , it's hard digest scaladoc.

can provide simple read/write example?

you can utilize scala.io.source read in tab delimited info file.

some sample data:

0 1 2 3 4 5 6 7 8 9 10 11

one of densematrix constructors has form new densematrix(rows: int, data: array[v], offset: int = 0) i'll utilize that.

get number of rows:

scala> scala.io.source.fromfile("tabdelimited.txt").getlines.size res 0:int = 2

then info array[int]:

scala> scala.io.source.fromfile("tabdelimited.txt").getlines.toarray.flatmap(_.split("\t")).map(_.toint) res1: array[int] = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

then res0 , res1 can used create new densematrix.

scala scalala scala-breeze

No comments:

Post a Comment