0

I need to generate many (100k order) random vectors of variable sizes such that:

sum(v_i)=0 and sum(v_i^2)=1

sum up to zero and the sum of the squares is one, for each vector.

This is the code I write:

c1 <- rnorm(ki[i])
c1 <- c1-mean(c1)
e1 <- c1/sqrt(sum(c1^2)) 

Where e1 is the final vector I need: sum(e1)=0 and sum(e1^2)=1

Is it possible to speed up this generation?

  • If the 'variable sizes' are from a small domain, you could generate all `n` replicates of a particular size at one go, e.g., `library(matrixStats); n = 1000; m = matrix(rnorm(n * 100), n); m = m - rowMeans(m); e = m / rowSds(m)` and loop over values of `n`. – Martin Morgan Jan 26 '16 at 11:57
  • Yes variable sizes stands for (approximately) 2-20. the problem is that for each single row (following what you said) there are different number of elements. I can fix that using some NA structure in matrix (I'm quite sure that means will works fine with na.rm=TRUE), but anyway it will need a loop this way, loosing all the advantage of vectorial form. Am I right? – Giovanni Romeo Jan 26 '16 at 12:27
  • Can you edit your question witht he entire problem? For instance, what is ki? What is i? – Heroka Jan 26 '16 at 13:04
  • Sorry for vagueness, ki is a vector that contains the lengths of vectors and i is the index of the main loop. `for(i in 1:n) { c1 <- rnorm(ki[i]) c1 <- c1-mean(c1) e1 <- c1/sqrt(sum(c1^2)) *thing to do with e1* }` – Giovanni Romeo Jan 26 '16 at 13:25
  • You could use a parallel for loop as the vectors are independent of each other. – Erwin Kalvelagen Jan 26 '16 at 14:01
  • All this is itself inside a parallel loop, i don't think it's a good idea to nest parallel loops... – Giovanni Romeo Jan 26 '16 at 15:12

0 Answers0