-1

I am writing a for loop accumulation in R. Why the final result is zero?

Is there anything I can do to fix it? thanks.

mse =numeric()

for (i in 1:nrow(m1$v)){ 

   i_d = 128-i
    for (j in 1:ncol(m1$v)){
      
      j_d = 128-j
      lam_hij = m1$v[i,j]
      lam_ij = km[i_d,j_d]
      mse_ = ( lam_hij -lam_ij )^2
      if (is.na(mse_)&&isTRUE(is.na(mse_))){mse_=0}
      mse = mse + mse_
      
      
    
    }
   i = i+1
       
}
mse

I would like to see something other than zero

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Did you check to see if `1+numeric()` gave you 1? (I cannot check on my iPhone. ) – IRTFM Jan 16 '23 at 07:13
  • `mse = mse + mse_` is adding the value of `mse_` to all elements of a zero-length vector (`mse`) which results in a zero length vector. Try either initialising as `mse <- 0` instead of using `numeric()` or if you want to capture the value of of `mse_` in each loop, use `mse <- c(mse, mse_)` – Paul Stafford Allen Jan 16 '23 at 07:44

2 Answers2

3

Two problems:

  1. you don't show data.
  2. you don't show output

I think what is probably happening can be see in this tiny bit of console dialog:

 > 1+numeric()
 numeric(0)

So the result is not actually zero but rather an empty (length=0) numeric vector. Adding something to nothing is arguably undefined and so one might have expected NA as the result, but certainly not 1 in this instance.

You could try setting mse to 0 and re-running your code.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • ["As from R 1.4.0, any arithmetic operation involving a zero-length vector has a zero-length result."](https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Recycling-rules) – Roland Jan 16 '23 at 07:37
  • @Roland No disagreement. Was only trying to say that the initialization should have been to a specific value, most probably 0. I wonder if other languages have a different convention? – IRTFM Jan 16 '23 at 09:38
  • I only pointed this out because I naively would have expected an error. – Roland Jan 16 '23 at 09:41
  • @Roland Or at least a warning. Creation of NAs produces a warning. – IRTFM Jan 16 '23 at 10:44
  • Hence I have posted this on R-devel. But I don't expect it to be changed. – Roland Jan 16 '23 at 11:47
  • I saw that and cast my vote with yours. – IRTFM Jan 16 '23 at 11:59
0

I am not answering at your question as someone did it above I guess.

But considering you code, here is an advice. You should create vector, matrice etc outside the loop and then fill them inside the loop. This to avoid too long computation time while launching some very long loop.

See below what I mean as a simple example.

    #your method
    x <- 0
    system.time(
    for(i in 2:10000)
    {
    x <- c(x,x[i-1]+i)
    }
    )
utilisateur     système      écoulé 
       0.10        0.07        0.16 

    #better 
    z <- rep(NA,10000)
    z[1] <- 0
    system.time(
    for(i in 2:10000)
    {
    z[i] <- z[i-1]+i
    }
    )
utilisateur     système      écoulé 
          0           0           0 
    all(x == z)
    TRUE

Your method takes a little bit of time, while it should not.

VincentP
  • 89
  • 10