Re: [R] problem with nested loops

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Wed, 30 Jul 2008 12:34:25 +0300

What you need is chapter 9 in
http://cran.r-project.org/doc/manuals/R-intro.pdf . And you could also use chapter 2, especially 2.2.

> specdist <-matrix(NA,nrow=40,ncol=20)
>
> for (j in 1:40){
> for(i in 1:20){
>
specdist[j,i]<-sqrt((pnr[i,1]-mvp[i,1])^2+(pnr[i,2]-mvp[i,2])^2)+....+(pnr[i,14]-mvp[i,14])^2))
> }
> }
> but it only repeats from 1:40 and does not step from 41 to 80; from 81 to
40 etc. certainly there is a "if" "else" "next" "step??" or "while"
> missing, but I never implemented one of those. :(

It does exactly what you've told it to do. And if(you really insist on using loops) it would be easy to add another loop that would go from 0 to 40 to 80 etc:
for(another.unnecessary.loop in seq(0,800,40){ # and now add that one to i for pnr

And things like dist() might make calculating distance matrices /see: help(dist) / a bit easier. As you see from documentation, it calculates distances between rows of a matrix; so you can first rbind your 2 matrices, then use dist, and then extract the part you want. Or at least you might want to consider making your calculations simpler by using vectors in a decent way:

sqrt(sum((pnr[i,]-mvp[i,])^2 )) # instead of adding them one by one

good luck,
Kenn

On Wed, Jul 30, 2008 at 10:49 AM, Jens Oldeland <oldeland_at_gmx.de> wrote:

> Dear all,
>
> I have a problem with constructing a nested loop.
>
> I have two matrices:
>
> pnr:
> 800 rows 14 columns
>
> where rows are 40x20 meaning that 40 rows belong to one of twenty objects
> in the matrix pnr
>
> mvp:
> 20 rows and 14 columns
>
> I want to:
> calculate a distance value with the first 40 rows of pnr but 20times with
> the 1st row of mvp
>
> then the next 40 rows of pnr and 20times with the second row of mvp and so
> on. in total 20 times.
>
> i tried with:
>
>
> > dim(p)
> [1] 800 14
> > dim(mvp)
> [1] 20 14
>
> specdist <-matrix(NA,nrow=40,ncol=20)
>
> for (j in 1:40){
> for(i in 1:20){
>
> specdist[j,i]<-sqrt((pnr[i,1]-mvp[i,1])^2+(pnr[i,2]-mvp[i,2])^2)+....+(pnr[i,14]-mvp[i,14])^2))
> }
> }
>
> but it only repeats from 1:40 and does not step from 41 to 80; from 81 to
> 40 etc. certainly there is a "if" "else" "next" "step??" or "while" missing,
> but I never implemented one of those. :(
>
> I would be very grateful for any help!
> thank you
>
> Jens
>
> ______________________________________________
> R-help_at_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.
>

        [[alternative HTML version deleted]]



R-help_at_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. Received on Wed 30 Jul 2008 - 09:40:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 Jul 2008 - 10:33:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive