[R] subloop - flexible coding - variable number of loops

Peter Jaksons
Thu, 27 Jan 2011 23:29:47 -0000

Hello,



I want to create a flexible code for the following example: In stead of
using a different code for each n (as in my example below), I want to
write a general code for every n (n from 1 to 10).

I tried a lot of things, but I always got stuck with the number of
subloops or witch closing the brackets. Any suggestions?



Cheers,



Peter



Department of Mathematics and Statistics

University of Canterbury

New Zealand





n <-2



matrixallo <- matrix(nrow=0,ncol=n)



for(i in 1:20)

    {

    for (j in 1:20)

      {

      if (i+j == 20)

        {

        newentry<- cbind(i,j)

        matrixallo <- rbind(matrixallo,newentry)

        }

      }

    }





n <-3



matrixallo <- matrix(nrow=0,ncol=n)



  for(i in 1:20)

    {

    for (j in 1:20)

      {

      for (k in 1:20)

        {

        if (i+k+j == 20)

          {

          newentry<- cbind(i,j,k)

          matrixallo <- rbind(matrixallo,newentry)

          }

        }

      }

    }


This email may be confidential and subject to legal privilege, it may
not reflect the views of the University of Canterbury, and it is not
guaranteed to be virus free. If you are not an intended recipient,
please notify the sender immediately and erase all copies of the message
and any attachments.

Please refer to http://www.canterbury.ac.nz/emaildisclaimer for more
information.

	[[alternative HTML version deleted]]