[R] replace zeros in a block diagonal matrix with small random values?

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Feb 24 14:33:39 CET 2009

say 'mat' is your matrix, then you could try something like (untested):

ind <- mat == 0
mat[ind] <- runif(sum(ind))

I hope it helps.


Rick DeShon wrote:
> Hi All.
> Imagine you have a large block diagonal matrix.  I'd like to replace
> the zeros in this matrix with small random (runif) numbers.  Any ideas
> for a simple and efficient way to do this?
> Best regards,
> Rick DeShon
> ______________________________________________
> R-help at 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.

Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

More information about the R-help mailing list