That may be a better solution, but I don't think
it is clearly a better solution.
I presume you mean that your computation is the
most time efficient. That seems believable to me.
It is not the most human efficient -- it will take
some one reading the code non-trivial effort to
understand it.
Whether time or code clarity are more important
depends on the particular application.
Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
[EMAIL PROTECTED] wrote:
It seems that this solution provided by Dan (and also available in
SPoetry; I'm sorry I didn't notice it) is the fastest and simplest. I was
using a more standard approach:
V <- t(A)[(0:(nrow(A)-1))*ncol(A)+X],
That wasn't bad, but I was confident that you, R gurus, could outperform
this. This is clearly a much better solution.
Thanks all, and best wishes,
Javier
------
on 08/09/2008 06:52 AM Dan Davison wrote:
On Sat, Aug 09, 2008 at 06:29:59AM -0500, Marc Schwartz wrote:
on 08/09/2008 06:01 AM [EMAIL PROTECTED] wrote:
Hi;
If we have a matrix A, and a vector X, where length(X)=nrow(A), and X
contains a wanted column for each row in A, in row ascending order.
How
would be the most effective way to extract the desired vector V (with
length(V)=nrow(A))?
A <- matrix(1:20, 4, 5)
A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
# Create an arbitrary set of indices, one for each row in A
X <- c(2, 5, 1, 4)
X
[1] 2 5 1 4
Presumably you want:
V <- c(A[1, 2], A[2, 5], A[3, 1], A[4, 4])
V
[1] 5 18 3 16
If so, then:
sapply(seq(nrow(A)), function(i) A[i, X[i]])
[1] 5 18 3 16
Or
A[cbind(seq(nrow(A)), X)]
[1] 5 18 3 16
Dan
Better (and faster) solution Dan.
I can't blame the lack of coffee on missing that one this morning. I
have had a full pot already over the past 6 hours, working on shifting
my internal clock and getting ready to begin my journey to Dortmund
later tonight...
Safe travels to all who are going.
Marc
______________________________________________
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.
______________________________________________
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.