Dear all,

I am pretty sure that this has been discussed before. Unfortunately, I 
can not find anything in the archives -- probably because I am 
"RSiteSearching" for the wrong terms. If I remember correctly, I think I 
even asked this question a few years ago. But I cannot even find this.

The basic problem is that a result depends on a previous result. This is 
easy with a loop--but how can I do this without a loop?

Lets give an example:

initial.matrix <- rbind(rep(1,4), matrix(0,ncol=4,nrow=5))
the.other.matrix <- matrix(runif(20), ncol=4, nrow=5)

the initial matrix should be filled according to the following 
(pseudo-code) rule:
if (row==1) initial.matrix[1,] <- 1
if (row>1) initial.matrix[x,] <- initial.matrix[x-1,] * 
the.other.matrix[x-1,]

as I said this is easy to do with a loop:
for (i in 2:(nrow(initial.matrix))) {
   initial.matrix[i,] <- initial.matrix[i-1,]*the.other.matrix[i-1,]
}
initial.matrix

But how can I do this without a loop?

Thank you already in advance!
Roland

______________________________________________
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.

Reply via email to