Eliminate advance indexing

By converting the matrix to vector, this will eliminate advanced indexing which is very expensive on gpu.