vector-matrix multiplication in r -


i want multiply 1000 random variables matrix 1000 different resultant matrices.

i'm running following code :

threshold <- runif(1000,min=0,max=1)    #generating 1000 random variables can see 1000 multiple results of burstscore burstscore <- matrix(data=0,nrow=nrow(fm2),ncol=ncol(fpre2)) #calculating final burst score (i in 1:nrow(fm2)){   (j in 1:ncol(fpre)){    #dimentions of matrices    (fpre,fm,growth,td,burstscore) 432,24   {   burstscore[i,j]= ((as.numeric(threshold))*(as.numeric(growth[i,j]))) + ((1-(as.numeric(threshold)))*(as.numeric(td[i,j]))) }   } } 

i'm getting following error -

'error in burstscore[i, j] = ((as.numeric(threshold)) * (as.numeric(growth[i,  :    number of items replace not multiple of replacement length' 

you trying put in 1 cell of burstscore matrix 1000 values (as multiplying each [i,j] 1 entire "threshold" vector). apart this, code contains unnecesary elements (brackets or as.numeric() statements). and, of course, said above, not reproducible, , had "invent" several matrices.

i guess want following:

threshold <- runif(1000,min=0,max=1) growth <- matrix(runif(432*24), ncol=24)  burstscore <- vector("list", length(threshold)) (i in 1:length(threshold)) {     burstscore[[i]]= (threshold[i]*growth) + ((1-threshold[i])*td) } 

in r, more elegant use lapply() function:

burstscore <- lapply(threshold, function(x) (x*growth)+((1-x)*td)) 

finally, suggest put more meaningful title question, potentially helpful others also.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -