[R] Matrix building to remove for loops

From: Brian Pellerin <brianpatrickpellerin_at_gmail.com>
Date: Tue, 15 Mar 2011 19:51:09 -0300


Hello R users,

I would like to reduce the number of for loops in my code. I build these matrices (50000 times). The main diagonal are 1s and the two sides along the main diagonal mirror each other as follows:

i<-5
fi<-matrix(0,nrow=i,ncol=i)#floral inhibition matrix for(r in 1:i){ for(c in
1:i){ if(r==c){ fi[r,c]<-1 }else if(r<c){ fi[r,c]<-1-runif(1)^.5 }else{ fi[r,c]<-fi[c,r] } } }
fi

Any thoughts?

Sincerely,
Brian

        [[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 16 Mar 2011 - 00:58:44 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 16 Mar 2011 - 01:30:22 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