[R] matrix dimension and for loop
Laurent Gautier
laurent at genome.cbs.dtu.dk
Tue Apr 9 19:16:08 CEST 2002
On Tue, Apr 09, 2002 at 07:12:08PM +0200, Peter Dalgaard BSA wrote:
> Laurent Gautier <laurent at genome.cbs.dtu.dk> writes:
>
> > On Tue, Apr 09, 2002 at 05:46:40AM -0700, sonchawan tamkaew wrote:
> > > Dear all,
> > >
> > > My questions are that if I have
> > >
> > > > x<-rnorm(50)
> > > > dim(x)<-c(10,5)
> > > > y=1:5
> > > > Z<-matrix(0,NROW(x),NROW(y))
> > > > for (j in 1:NROW(y)) Z[,j]<-x[,j]*y[j]
> > >
> > > 1. Is there any other way to write this without 'for'
> > > loop?
> >
> >
> > Z <- x * y
> >
> > seems to do the job....
>
> Nope. Try it with x <- matrix(1,10,5) and see.
...right... it was 'y[j]' and not 'y[,j]' (blurry eyes... looking too long at the screen)
this is probably what everybody already gave:
(I receive r-help few hours before the first ones to receive it)
x * matrix(y, ncol=ncol(x), nrow=nrow(x), byrow=T)
(I hope it work for good now... :) )
L.
>
> --
> O__ ---- Peter Dalgaard Blegdamsvej 3
> c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list