Created
February 22, 2013 17:16
-
-
Save aldente39/5015075 to your computer and use it in GitHub Desktop.
Read a Matrix Market file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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