Re: [R] Extracting from a matrix w/o for-loop

2006-07-29 Thread Adrian DUSA
Hi,

On Friday 28 July 2006 20:21, Horace Tso wrote:
 Unless there is another level of complexity that i didn't see here,
 wouldn't it be a simply application of sapply as follow,

 sapply( 1:dim(M2)[[1]], function(x) M1[M2[x,1], M2[x,2]] )

Andy's previous answer involving matrix indexing (M1[M2]) is simpler but just 
for the sake of it, since we're dealing with matrices it is not a case of 
sapply but of _apply_:

apply(M2, 1, function(x) M1[x[1], x[2]])

My 2c,
Adrian

-- 
Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Romania
Tel./Fax: +40 21 3126618 \
  +40 21 3120210 / int.101

__
R-help@stat.math.ethz.ch 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] Extracting from a matrix w/o for-loop

2006-07-28 Thread Camarda, Carlo Giovanni
Dear R-users,

likely there is a simple solution for this problem, but I currently
cannot see it.

I basically would like to get from a matrix values in particular
positions which are the rows of another matrix, without using a
for-loop.

In other words: is there any way to avoid the for-loop in the following
simple example:

M1 - matrix(1:20, ncol=2)
M2 - rbind(c(1,1), c(2,1), c(3,2), c(5,2), c(8,1))
v - numeric(nrow(M2))
for(i in 1:length(v)){
v[i] - M1[M2[i,1], M2[i,2]]
}

Any suggestion would be welcome,

Ciao,
Carlo Giovanni Camarda

===
Camarda Carlo Giovanni
PhD-Student
Max Planck Institute for Demographic Research
Konrad-Zuse-Strasse 1
18057 Rostock, Germany
Tel:  +49 (0)381 2081 172
Fax: +49 (0)381 2081 472
[EMAIL PROTECTED]
===


--
This mail has been sent through the MPI for Demographic Rese...{{dropped}}

__
R-help@stat.math.ethz.ch 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.


Re: [R] Extracting from a matrix w/o for-loop

2006-07-28 Thread Liaw, Andy
Yes, by matrix indexing:

R M1[M2]
[1]  1  2 13 15  8
R v
[1]  1  2 13 15  8

Andy 

From: Camarda, Carlo Giovanni
 
 Dear R-users,
 
 likely there is a simple solution for this problem, but I currently
 cannot see it.
 
 I basically would like to get from a matrix values in particular
 positions which are the rows of another matrix, without using a
 for-loop.
 
 In other words: is there any way to avoid the for-loop in the 
 following
 simple example:
 
 M1 - matrix(1:20, ncol=2)
 M2 - rbind(c(1,1), c(2,1), c(3,2), c(5,2), c(8,1))
 v - numeric(nrow(M2))
 for(i in 1:length(v)){
 v[i] - M1[M2[i,1], M2[i,2]]
 }
 
 Any suggestion would be welcome,
 
 Ciao,
 Carlo Giovanni Camarda
 
 ===
 Camarda Carlo Giovanni
 PhD-Student
 Max Planck Institute for Demographic Research
 Konrad-Zuse-Strasse 1
 18057 Rostock, Germany
 Tel:  +49 (0)381 2081 172
 Fax: +49 (0)381 2081 472
 [EMAIL PROTECTED]
 ===
 
 
 --
 This mail has been sent through the MPI for Demographic 
 Rese...{{dropped}}
 
 __
 R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.


Re: [R] Extracting from a matrix w/o for-loop

2006-07-28 Thread Horace Tso
Unless there is another level of complexity that i didn't see here,
wouldn't it be a simply application of sapply as follow,

sapply( 1:dim(M2)[[1]], function(x) M1[M2[x,1], M2[x,2]] )

Hope this helps.

Horace



 Camarda, Carlo Giovanni [EMAIL PROTECTED] 7/28/2006 9:40 AM

Dear R-users,

likely there is a simple solution for this problem, but I currently
cannot see it.

I basically would like to get from a matrix values in particular
positions which are the rows of another matrix, without using a
for-loop.

In other words: is there any way to avoid the for-loop in the
following
simple example:

M1 - matrix(1:20, ncol=2)
M2 - rbind(c(1,1), c(2,1), c(3,2), c(5,2), c(8,1))
v - numeric(nrow(M2))
for(i in 1:length(v)){
v[i] - M1[M2[i,1], M2[i,2]]
}

Any suggestion would be welcome,

Ciao,
Carlo Giovanni Camarda

===
Camarda Carlo Giovanni
PhD-Student
Max Planck Institute for Demographic Research
Konrad-Zuse-Strasse 1
18057 Rostock, Germany
Tel:  +49 (0)381 2081 172
Fax: +49 (0)381 2081 472
[EMAIL PROTECTED] 
===


--
This mail has been sent through the MPI for Demographic
Rese...{{dropped}}

__
R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.