scala alternative for using index variable -


suppose have matrix, has basis column , non-basis column. , need ability obtain basis part of matrix or non-basis. colums bases specified in array declared var base: array[boolean]

currently i'm doing like:

  def getmatrix(matrix: densematrix[double], pred: boolean, m: int, n: int): densematrix[double] = {     var = densematrix.zeros[double](m, n)     var scanpos: int = 0     var insertpos: int = 0     (el <- base) {       if (el == pred) {         a(::, insertpos) := matrix(::, scanpos)         insertpos += 1       }       scanpos += 1     }     return   } 

but code ugly , hate it. there must more elegant solution , i'm asking it

ps: densematrix class breeze library , may considered 2-d array

i'm not sure if breeze offers better way this, this:

  def getmatrix(matrix: densematrix[double], pred: boolean, m: int, n: int): densematrix[double] = {     val data = base       .zipwithindex       .filter(_._1 == pred)       .map(b => matrix(::, b._2).toarray)       .flatten      new densematrix(m, n, data)   } 

something should work too:

  def getmatrix(matrix: densematrix[double], pred: boolean, m: int, n: int): densematrix[double] = {     val = densematrix.zeros[double](m, n)      {       x <- base.zipwithindex.filter(_._1 == pred).zipwithindex       ((el, matrixindex), newindex) = x     } yield a(::, newindex) := matrix(::, matrixindex)        } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -