Nicolas Gutierrez
2010-Sep-15 23:31 UTC
[R] how to superimpose 2 matrices of different sizes
Hi All, I?m trying to superimpose (or "add") two matrices: 1. resulting from a table function with frequencies: > U=table(pop$xloc, pop$yloc)) 14 15 16 17 18 19 20 21 22 5 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 9 0 5 10 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 2. The second created to match my spatial grid (20x25): > Uempty=matrix(data = 0, nrow = 20, ncol = 25) I want to place each element of U (e.g. U[22,9]=5) in the corresponding cell of Uempty and end up with this final matrix like this: 1 2 3 ... 22 23 24 25 5 0 0 0 ... 0 0 0 0 : : : : ... : : : : 7 0 0 0 ... 0 0 0 0 8 0 0 0 ... 0 0 0 0 9 0 0 0 ... 5 0 0 0 : : : : ... : : : : 20 0 0 0 ... 0 0 0 0 Any ideas? Thanks!!! Nico
Jorge Ivan Velez
2010-Sep-15 23:38 UTC
[R] how to superimpose 2 matrices of different sizes
Hi Nicolas, Try (untested): with(pop, table(factor(xloc, levels = 1:20), factor(loc, levels = 1:25))) HTH, Jorge On Wed, Sep 15, 2010 at 7:31 PM, Nicolas Gutierrez <> wrote:> Hi All, > > I’m trying to superimpose (or "add") two matrices: > > 1. resulting from a table function with frequencies: > > > U=table(pop$xloc, pop$yloc)) > > 14 15 16 17 18 19 20 21 22 > 5 0 0 0 0 0 0 0 0 0 > 7 0 0 0 0 0 0 0 0 0 > 8 0 0 0 0 0 0 0 0 0 > 9 0 0 0 0 0 0 9 0 5 > 10 0 0 0 0 0 0 0 0 0 > 11 0 0 0 0 0 0 0 0 0 > > 2. The second created to match my spatial grid (20x25): > > > Uempty=matrix(data = 0, nrow = 20, ncol = 25) > > I want to place each element of U (e.g. U[22,9]=5) in the corresponding > cell of Uempty and end up with this final matrix like this: > > 1 2 3 ... 22 23 24 25 > 5 0 0 0 ... 0 0 0 0 > : : : : ... : : : : > 7 0 0 0 ... 0 0 0 0 > 8 0 0 0 ... 0 0 0 0 > 9 0 0 0 ... 5 0 0 0 > : : : : ... : : : : > 20 0 0 0 ... 0 0 0 0 > > Any ideas? > > Thanks!!! > > Nico > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Hi: Try something like this: ind <- as.matrix(expand.grid(rownames(U), colnames(U))) Uempty[ind] <- U[ind] ind should be a two column matrix of (row, col) indices. Then just replace the values of Uempty associated with those indices with the corresponding entries of U. HTH, Dennis On Wed, Sep 15, 2010 at 4:31 PM, Nicolas Gutierrez <nicolasg@uw.edu> wrote:> Hi All, > > I’m trying to superimpose (or "add") two matrices: > > 1. resulting from a table function with frequencies: > > > U=table(pop$xloc, pop$yloc)) > > 14 15 16 17 18 19 20 21 22 > 5 0 0 0 0 0 0 0 0 0 > 7 0 0 0 0 0 0 0 0 0 > 8 0 0 0 0 0 0 0 0 0 > 9 0 0 0 0 0 0 9 0 5 > 10 0 0 0 0 0 0 0 0 0 > 11 0 0 0 0 0 0 0 0 0 > > 2. The second created to match my spatial grid (20x25): > > > Uempty=matrix(data = 0, nrow = 20, ncol = 25) > > I want to place each element of U (e.g. U[22,9]=5) in the corresponding > cell of Uempty and end up with this final matrix like this: > > 1 2 3 ... 22 23 24 25 > 5 0 0 0 ... 0 0 0 0 > : : : : ... : : : : > 7 0 0 0 ... 0 0 0 0 > 8 0 0 0 ... 0 0 0 0 > 9 0 0 0 ... 5 0 0 0 > : : : : ... : : : : > 20 0 0 0 ... 0 0 0 0 > > Any ideas? > > Thanks!!! > > Nico > > ______________________________________________ > 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. >[[alternative HTML version deleted]]