-5

I have problem with the efficiency of the following code:

g=0

for (i in 1:n){

  for (j in 1:m){

    g=g+(as.numeric(x[i]>y[j])*as.numeric(z[i]<=a)*as.numeric(h[j]>a))

  }
}

Thank you for your help.

d.b
  • 32,245
  • 6
  • 36
  • 77

1 Answers1

0
t(h > a) %*% sapply(x, ">", y) %*% (z <= a)
Ryan
  • 934
  • 8
  • 14
  • For small data it works, but for my case I faced the following error message: Error: cannot allocate vector of size 18.6 Gb – Shuluka Feb 27 '17 at 07:46
  • This means you don't have enough memory on your system. I don't think there's any way to increase efficiency without constructing some intermediate vectors / matrices (which then take up memory) in order to utilize linear algebra libraries. You may be able to increase the memory limit for R on your system. See http://stackoverflow.com/questions/1395229/increasing-or-decreasing-the-memory-available-to-r-processes – Ryan Feb 27 '17 at 15:56