The following code is slow and ugly:
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-pt$x[ver[i,j]]
y[count]<-pt$y[ver[i,j]]
z[count]<-pt$z[ver[i,j]]
}
Please help me make it better.
Thanks!
Bill
ugly loop
8 messages · Dimitris Rizopoulos, Marc Schwartz, Bill Simpson
maybe you want something like this: x <- c(t(pt$x)) Best, --D ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Bill Simpson" <William.Simpson at drdc-rddc.gc.ca> To: "r-help" <r-help at stat.math.ethz.ch> Sent: Friday, April 22, 2005 2:58 PM Subject: [R] ugly loop
The following code is slow and ugly:
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-pt$x[ver[i,j]]
y[count]<-pt$y[ver[i,j]]
z[count]<-pt$z[ver[i,j]]
}
Please help me make it better.
Thanks!
Bill
______________________________________________ R-help at 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
To clarify: I want to get rid of the loop over i,j
Here is a simpler example. ver is a 2D matrix
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-ver[i,j]
}
Bill
On Fri, 2005-04-22 at 08:58 -0400, Bill Simpson wrote:
The following code is slow and ugly:
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-pt$x[ver[i,j]]
y[count]<-pt$y[ver[i,j]]
z[count]<-pt$z[ver[i,j]]
}
Please help me make it better.
Thanks!
The following should work:
ver <- matrix(sample(1:16, 16), ncol = 4) pt <- data.frame(x = sample(1:16, 16),
+ y = sample(1:16, 16), + z = sample(1:16, 16))
ver
[,1] [,2] [,3] [,4] [1,] 8 9 5 13 [2,] 14 16 1 10 [3,] 12 2 11 7 [4,] 6 3 4 15
pt
x y z 1 6 15 15 2 9 2 3 3 11 1 5 4 14 4 10 5 13 7 14 6 1 14 7 7 15 10 4 8 10 5 12 9 4 12 2 10 8 8 13 11 16 11 1 12 7 13 9 13 2 16 11 14 3 9 16 15 5 6 8 16 12 3 6
x <- pt$x[ver] y <- pt$y[ver] z <- pt$z[ver]
x
[1] 10 3 7 1 4 12 9 11 13 6 16 14 2 8 15 5
y
[1] 5 9 13 14 12 3 2 1 7 15 11 4 16 8 10 6
z
[1] 12 16 9 7 2 6 3 5 14 15 1 10 11 13 4 8 Keep in mind that a matrix is a vector with dims, so you can fill a vector from the matrix simply by doing the indexing with a single value, which will do the fill indexed column by column. HTH, Marc Schwartz
then use x <- c(t(ver)) Best, --D ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Bill Simpson" <William.Simpson at drdc-rddc.gc.ca> To: "r-help" <r-help at stat.math.ethz.ch> Sent: Friday, April 22, 2005 3:17 PM Subject: Re: [R] ugly loop
To clarify: I want to get rid of the loop over i,j
Here is a simpler example. ver is a 2D matrix
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-ver[i,j]
}
Bill
______________________________________________ R-help at 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
Thanks Marc for your help.
The following code is slow and ugly:
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-pt$x[ver[i,j]]
y[count]<-pt$y[ver[i,j]]
z[count]<-pt$z[ver[i,j]]
}
Please help me make it better.
Thanks!
The following should work:
ver <- matrix(sample(1:16, 16), ncol = 4) pt <- data.frame(x = sample(1:16, 16),
+ y = sample(1:16, 16), + z = sample(1:16, 16))
ver
[,1] [,2] [,3] [,4] [1,] 8 9 5 13 [2,] 14 16 1 10 [3,] 12 2 11 7 [4,] 6 3 4 15
pt
x y z 1 6 15 15 2 9 2 3 3 11 1 5 4 14 4 10 5 13 7 14 6 1 14 7 7 15 10 4 8 10 5 12 9 4 12 2 10 8 8 13 11 16 11 1 12 7 13 9 13 2 16 11 14 3 9 16 15 5 6 8 16 12 3 6
x <- pt$x[ver] y <- pt$y[ver] z <- pt$z[ver]
This doesn't give the same results as my original code -- it scrambles the order. OK I will explain my example. pts contains the x, y, z coordinates of some 3D points. These points are the vertices of 3D triangles. ver contains the indexes into pts. each line of ver contains 3 vertices -- they are the corners of a triangle. For example, if line 1 of ver is 10 9 7 That means I need to draw a triangle whose coordinates are pt$x[10],pt$y[10],pt$z[10] pt$x[9],pt$y[9],pt$z[9] pt$x[7],pt$y[7],pt$z[7] Now it should be clear why the ordering is critical. I am using rgl.triangles() to plot. It requires the x,y,z coordinates in the order I gave in my original code. Cheers Bill
On Fri, 2005-04-22 at 09:31 -0400, Bill Simpson wrote:
Thanks Marc for your help.
The following code is slow and ugly:
count<-0
for(i in 1:nrow(ver))
for(j in 1:ncol(ver))
{
count<-count+1
x[count]<-pt$x[ver[i,j]]
y[count]<-pt$y[ver[i,j]]
z[count]<-pt$z[ver[i,j]]
}
Please help me make it better.
Thanks!
The following should work:
ver <- matrix(sample(1:16, 16), ncol = 4) pt <- data.frame(x = sample(1:16, 16),
+ y = sample(1:16, 16), + z = sample(1:16, 16))
ver
[,1] [,2] [,3] [,4] [1,] 8 9 5 13 [2,] 14 16 1 10 [3,] 12 2 11 7 [4,] 6 3 4 15
pt
x y z 1 6 15 15 2 9 2 3 3 11 1 5 4 14 4 10 5 13 7 14 6 1 14 7 7 15 10 4 8 10 5 12 9 4 12 2 10 8 8 13 11 16 11 1 12 7 13 9 13 2 16 11 14 3 9 16 15 5 6 8 16 12 3 6
x <- pt$x[ver] y <- pt$y[ver] z <- pt$z[ver]
This doesn't give the same results as my original code -- it scrambles the order. OK I will explain my example. pts contains the x, y, z coordinates of some 3D points. These points are the vertices of 3D triangles. ver contains the indexes into pts. each line of ver contains 3 vertices -- they are the corners of a triangle. For example, if line 1 of ver is 10 9 7 That means I need to draw a triangle whose coordinates are pt$x[10],pt$y[10],pt$z[10] pt$x[9],pt$y[9],pt$z[9] pt$x[7],pt$y[7],pt$z[7] Now it should be clear why the ordering is critical. I am using rgl.triangles() to plot. It requires the x,y,z coordinates in the order I gave in my original code.
That's what I get for not comparing your results against my own. I just noted that you are going by row and not by column. So, using the same data above:
count<-0 for(i in 1:nrow(ver))
+ for(j in 1:ncol(ver))
+ {
+ count<-count+1
+ x[count]<-pt$x[ver[i,j]]
+ y[count]<-pt$y[ver[i,j]]
+ z[count]<-pt$z[ver[i,j]]
+ }
x
[1] 10 4 13 2 3 12 6 8 7 9 16 15 1 11 14 5
y
[1] 5 12 7 16 9 3 15 8 13 2 11 10 14 1 4 6
z
[1] 12 2 14 11 16 6 15 13 9 3 1 4 7 5 10 8 Thus, I just need to use t(ver) instead of ver:
x <- pt$x[t(ver)] y <- pt$y[t(ver)] z <- pt$z[t(ver)]
x
[1] 10 4 13 2 3 12 6 8 7 9 16 15 1 11 14 5
y
[1] 5 12 7 16 9 3 15 8 13 2 11 10 14 1 4 6
z
[1] 12 2 14 11 16 6 15 13 9 3 1 4 7 5 10 8 That should do it? HTH, Marc <Off to make another pot of coffee....>
On Fri, 22 Apr 2005, Marc Schwartz wrote:
Thus, I just need to use t(ver) instead of ver:
x <- pt$x[t(ver)] y <- pt$y[t(ver)] z <- pt$z[t(ver)]
That should do it?
Yep!! Thanks very much Marc and others who suggested this. Cheers Bill