Hello R users I would like to ask if anyone knows a computationally fast solution to this problem: I have an original matrix and an index matrix. The original matrix is ca 4000x4000 cells, and the index matrix has 261 unique values. From these, I want to produce a new matrix. Consider the following simplified example: Original matrix 1 4 6 5 3 4 8 5 2 4 7 8 9 8 3 6 index matrix 1 5 2 7 3 2 4 5 5 3 6 7 7 5 8 3 9 9 my current code is something like this for(i in 1:9) { changeVal <- which(originalMat==indexMat[i,1]) finalMat <- indexMat[i,2] } the output would look like this: Final matrix 5 5 7 3 2 5 3 3 7 5 5 3 9 3 2 7 At the moment it takes a while to process. Does anyone have any suggestions? reagrds Robert ********************************DISCLAIMER******************...{{dropped}}
George, Charles R wrote:> Hello R users > > I would like to ask if anyone knows a computationally fast solution to this problem: > > I have an original matrix and an index matrix. The original matrix is ca 4000x4000 cells, and the index matrix has 261 unique values. From these, I want to produce a new matrix. > > Consider the following simplified example: > > Original matrix > 1 4 6 5 > 3 4 8 5 > 2 4 7 8 > 9 8 3 6 > > index matrix > 1 5 > 2 7 > 3 2 > 4 5 > 5 3 > 6 7 > 7 5 > 8 3 > 9 9 > > my current code is something like this > > for(i in 1:9) { > changeVal <- which(originalMat==indexMat[i,1]) > finalMat <- indexMat[i,2] > }replace(originalMat, indexMat[,1], indexMat[,2]) Uwe Ligges> the output would look like this: > > Final matrix > 5 5 7 3 > 2 5 3 3 > 7 5 5 3 > 9 3 2 7 > > At the moment it takes a while to process. Does anyone have any suggestions? > > reagrds > > Robert > > > ********************************DISCLAIMER******************...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On 4 Aug 2003 at 10:10, Uwe Ligges wrote:> George, Charles R wrote: > ><snip>> replace(originalMat, indexMat[,1], indexMat[,2])Shouldnt that be originalmat <- replace(originalMat, indexMat[,1], indexMat[,2]) ? Kjetil Halvorsen> > Uwe Ligges > > > > the output would look like this: > > > > Final matrix > > 5 5 7 3 > > 2 5 3 3 > > 7 5 5 3 > > 9 3 2 7 > > > > At the moment it takes a while to process. Does anyone have any suggestions? > > > > reagrds > > > > Robert > > > > > > ********************************DISCLAIMER******************...{{dropped}} > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help