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
Post a Comment