Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-11 Thread Logickle
Thanks, guys. I'll take at a look at these solutions. BTW, my apologies, by A,B,C,D,... I meant just some real number, not characters A,B,..., but I wasn't clear about that. If that effects the solution approach please let me know, but I don't think it does. Thanks again, Doug Marc Schwartz-3

[R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread Logickle
Sorry, there may be some lingo for describing the extraction problem I have, but I don't know it. I have a matrix of 2 rows and N columns, and a vector of N 1s and 2s. Matrix M: 1 2 3 4 ... N 1A C D G ... 2B D F H ... Vector v:

Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread Marc Schwartz
On Jun 10, 2009, at 12:36 PM, Logickle wrote: Sorry, there may be some lingo for describing the extraction problem I have, but I don't know it. I have a matrix of 2 rows and N columns, and a vector of N 1s and 2s. Matrix M: 1 2 3 4 ... N 1A C D G

Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread Marc Schwartz
On Jun 10, 2009, at 6:26 PM, Marc Schwartz wrote: On Jun 10, 2009, at 12:36 PM, Logickle wrote: Sorry, there may be some lingo for describing the extraction problem I have, but I don't know it. I have a matrix of 2 rows and N columns, and a vector of N 1s and 2s. Matrix M: 1

Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread William Dunlap
Subscripting by a 2-column matrix M[cbind(v, seq_len(ncol(M)))] uses much less space (hence time) than making the ncol(M) by ncol(M) intermediate matrix just to extract its diagonal. E.g. test - function(n, seed) { if (!missing(seed)) set.seed(seed) M - matrix(sample(LETTERS,

Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread Marc Schwartz
On Jun 10, 2009, at 7:05 PM, William Dunlap wrote: Subscripting by a 2-column matrix M[cbind(v, seq_len(ncol(M)))] uses much less space (hence time) than making the ncol(M) by ncol(M) intermediate matrix just to extract its diagonal. E.g. test - function(n, seed) { if (!missing(seed))

Re: [R] How to extract from a matrix based on indices in a vector?

2009-06-10 Thread David Winsemius
On Jun 10, 2009, at 1:36 PM, Logickle wrote: Sorry, there may be some lingo for describing the extraction problem I have, but I don't know it. I have a matrix of 2 rows and N columns, and a vector of N 1s and 2s. Matrix M: 1 2 3 4 ... N 1A C D G