Skip to content

Instantly share code, notes, and snippets.

@aldente39
Created February 22, 2013 17:16
Show Gist options
  • Save aldente39/5015075 to your computer and use it in GitHub Desktop.
Save aldente39/5015075 to your computer and use it in GitHub Desktop.
Read a Matrix Market file
import scala.io._
def readmtx (file:String) = {
val source = Source.fromFile(file,"UTF-8").getLines.toList
val lines = source.filter(x => x.head != '%')
val info = ((lines(0)) split "[ ]+").map(x => x.toInt)
val mat = Array.ofDim[Double] (info(0), info(1))
for(i <- lines){
val tmp = (i split "[ ]+")
val (m,n,v) = (tmp(0).toInt-1, tmp(1).toInt-1, tmp(2).toDouble)
mat (m) (n) = v
}
mat
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment