

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
columnindices. 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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
> columnindices. 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I34127 Trieste
phone: +39 0 40 5 5837 68
email: [hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Try this:
diag(A[c(1,3),c(2,3)])
2010/3/12 Nils Rüfenacht < [hidden email]>:
> 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 columnindices. 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>

Henrique Dallazuanna
CuritibaParanáBrasil
25° 25' 40" S 49° 16' 22" O
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 columnindices. 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
Pass the indices in a matrix:
> A[matrix(c(c(1,3), c(2,3)), ncol=2)]
[1] 4 9

David
David Winsemius, MD
West Hartford, CT
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


On Mar 12, 2010, at 2:31 PM, David Winsemius wrote:
>
> 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 columnindices. 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
>
> Pass the indices in a matrix:
>
> > A[matrix(c(c(1,3), c(2,3)), ncol=2)]
> [1] 4 9
Also works for higher dimensions:
> A < array(seq(1,27),dim=c(3,3,3))
> A
, , 1
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
, , 2
[,1] [,2] [,3]
[1,] 10 13 16
[2,] 11 14 17
[3,] 12 15 18
, , 3
[,1] [,2] [,3]
[1,] 19 22 25
[2,] 20 23 26
[3,] 21 24 27
> matrix(1:3, ncol=3, nrow=3)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
> A[matrix(1:3, ncol=3, nrow=3)]
[1] 1 14 27

David.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
>columnindices. 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
>
>______________________________________________
> [hidden email] mailing list
> https://*stat.ethz.ch/mailman/listinfo/rhelp>PLEASE do read the posting guide http://*www.*Rproject.org/postingguide.html>and provide commented, minimal, selfcontained, reproducible code.


Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
9254231062
[hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

