khai wrote on 12/20/2011 01:24:10 PM: > Thanks very much for responses. The 2 suggestions each do a part of what I > want. Jean's proposal permutes nonzero elements within each row but shifts > the location of the nonzero element around in the j slot of each i row. And > Douglas's idea keeps the second part but permutes the entire x slot values. > I'll try to see how I can combine these 2 ideas but it's not immediately > obvious to me. =} > > -khai
I'm not sure what you mean by shifting "the location of the nonzero element around in the j slot of each i row." My code shifts the nonzero columns around at random, but keeps the zero column in its original location. I thought that this is what you wanted to do. In the example below, the zero column starts as column 5 and ends as column 5, and the nonzero columns are reordered. Columns 1, 2, 3, 4, 5, 6 end up as 2, 1, 6, 3, 5, 4. Jean > M1 5 x 6 sparse Matrix of class "dgTMatrix" [1,] . 6.569 5.592 . . . [2,] 1.163 2.688 . 1.85 . 3.217 [3,] 2.527 3.375 . . . 6.481 [4,] . . . . . 1.483 [5,] 1.004 . . . . . > nonzero.cols <- !apply(M1==0, 2, all) > M2 <- as.matrix(M1) > reord <- sample(seq(dim(M1)[2])[nonzero.cols]) > M2[, nonzero.cols] <- as.matrix(M1[, reord]) > Matrix(M2, sparse=TRUE) 5 x 6 sparse Matrix of class "dgCMatrix" [1,] 6.569 . . 5.592 . . [2,] 2.688 1.163 3.217 . . 1.85 [3,] 3.375 2.527 6.481 . . . [4,] . . 1.483 . . . [5,] . 1.004 . . . . [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.