Nils Rüfenacht
2010-Mar-12 14:26 UTC
[R] Getting multiple matrix-values using a single command
Dear all! I'm trying to get multiple values from a matrix by using a single command. Given a matrix A A <- matrix(seq(1,9),nrow=3,ncol=3) How can I get e.g. the values A[1,2] = 4 and A[3,3] = 9 with a single command and without using any loop? My first idea was to generate a row- and a column vector for the indices, i.e. c(1,3) indicating row number 1 (for A[1,2]) and row number 3 (for A[3,3]) and similar for column-indices. Then I've tried to call A[c(1,3),c(2,3)] but instead of 4 , 9 the result is [,1] [,2] [1,] 4 7 [2,] 6 9 Any suggestions? Regards, Nils
Claudia Beleites
2010-Mar-12 18:57 UTC
[R] Getting multiple matrix-values using a single command
use a matrix of n x 2 to index. For details: sec. 5.3 "Index matrices" in the introduction. HTH Claudia Nils R?fenacht wrote:> Dear all! > > I'm trying to get multiple values from a matrix by using a single command. > > Given a matrix A > > A <- matrix(seq(1,9),nrow=3,ncol=3) > > How can I get e.g. the values A[1,2] = 4 and A[3,3] = 9 with a single > command and without using any loop? My first idea was to generate a row- > and a column vector for the indices, i.e. c(1,3) indicating row number 1 > (for A[1,2]) and row number 3 (for A[3,3]) and similar for > column-indices. Then I've tried to call > > A[c(1,3),c(2,3)] > > but instead of 4 , 9 the result is > > [,1] [,2] > [1,] 4 7 > [2,] 6 9 > > Any suggestions? > > Regards, Nils > > ______________________________________________ > 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.-- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 0 40 5 58-37 68 email: cbeleites at units.it
Henrique Dallazuanna
2010-Mar-12 19:01 UTC
[R] Getting multiple matrix-values using a single command
Try this: diag(A[c(1,3),c(2,3)]) 2010/3/12 Nils R?fenacht <nils.ruefenacht at bluewin.ch>:> Dear all! > > I'm trying to get multiple values from a matrix by using a single command. > > Given a matrix A > > A <- matrix(seq(1,9),nrow=3,ncol=3) > > How can I get e.g. the values A[1,2] = 4 and A[3,3] = 9 with a single > command and without using any loop? My first idea was to generate a row- and > a column vector for the indices, i.e. c(1,3) indicating row number 1 (for > A[1,2]) and row number 3 (for A[3,3]) and similar for column-indices. Then > I've tried to call > > A[c(1,3),c(2,3)] > > but instead of 4 , 9 the result is > > [,1] [,2] > [1,] ? ?4 ? ?7 > [2,] ? ?6 ? ?9 > > Any suggestions? > > Regards, Nils > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
David Winsemius
2010-Mar-12 19:31 UTC
[R] Getting multiple matrix-values using a single command
On Mar 12, 2010, at 9:26 AM, Nils R?fenacht wrote:> Dear all! > > I'm trying to get multiple values from a matrix by using a single > command. > > Given a matrix A > > A <- matrix(seq(1,9),nrow=3,ncol=3) > > How can I get e.g. the values A[1,2] = 4 and A[3,3] = 9 with a > single command and without using any loop? My first idea was to > generate a row- and a column vector for the indices, i.e. c(1,3) > indicating row number 1 (for A[1,2]) and row number 3 (for A[3,3]) > and similar for column-indices. Then I've tried to call > > A[c(1,3),c(2,3)] > > but instead of 4 , 9 the result is > > [,1] [,2] > [1,] 4 7 > [2,] 6 9Pass the indices in a matrix: > A[matrix(c(c(1,3), c(2,3)), ncol=2)] [1] 4 9 -- David> > Any suggestions? > > Regards, Nils > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Don MacQueen
2010-Mar-13 01:37 UTC
[R] Getting multiple matrix-values using a single command
Everyone is being too complicated. c( A[1,2] , A[3,3] ) will do what you ask.> A <- matrix(seq(1,9),nrow=3)> c( A[1,2] , A[3,3] )[1] 4 9 But I would assume you have some more general problem in mind, and I do not know if this simple approach will meet those needs. -Don At 3:26 PM +0100 3/12/10, Nils R?fenacht wrote:>Dear all! > >I'm trying to get multiple values from a matrix by using a single command. > >Given a matrix A > >A <- matrix(seq(1,9),nrow=3,ncol=3) > >How can I get e.g. the values A[1,2] = 4 and >A[3,3] = 9 with a single command and without >using any loop? My first idea was to generate a >row- and a column vector for the indices, i.e. >c(1,3) indicating row number 1 (for A[1,2]) and >row number 3 (for A[3,3]) and similar for >column-indices. Then I've tried to call > >A[c(1,3),c(2,3)] > >but instead of 4 , 9 the result is > >[,1] [,2] >[1,] 4 7 >[2,] 6 9 > >Any suggestions? > >Regards, Nils > >______________________________________________ >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.-- --------------------------------- Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 macq at llnl.gov