nik
nik

Reputation: 2584

how to fix my function implementing interpolation

I have tried to make an interpolation function like below

Lets assume the so and op are values like

so <- seq(1,10, by=1)
op <- seq(10,20, by=1)



  myopt <- function(so, op) {
  # here I make two empty 
  ff <- c() # creates an empty df
  rr <- c() # creates an empty df
  fff <- c()
  inter = round(so[1]); # interpolation
  sid = round(so[2]); # sideways movement
  # if the lenght ff is bigger than 0
  library(pracma)
  if (length(ff) > 0){
    # then the step is 
    step <- (length(ff)+ inter)/(length(rr))
    fff <- interp1(ff,seq(1,length(ff), by=step))
  }  
  if (length(fff) >= length(rr)){
    fff = fff[1:length(rr)]
  } else {
    if (length(fff) < length(rr)){
      diff = length(rr)-length(fff)
      fff = c(fff,ones(1,diff)*ff(length(ff)))
    }
  }
  if (length(fff) > 0){
    if (sid > 0){
      mysid = ones(1,sid)*fff(1)
      ffff = c(mysid, fff[1:(length(fff) - sid)]);
    } else {
      if (sid <= 0) {
        sid = abs(sid);
        mysid = ones(1,sid)*ff(length(ff));
        ffff = c(fff[(sid+1):length(fff)], mysid);
      }
    }
  }
  # normalizing and scaling
  rrs <- (Re(rr)/max(Re(rr)))   
  fffs <- (Re(ffff)/max(Re(ffff)))
  # evaluation by correlation coefficient 
  if ((norm(rrs-mean(rrs)))* (norm(ffs-meas(ffs)))==0) {
    cc <- -1e10 
  } else {
    cc =((rrs-mean(rrs))*t((ffs-meas(ffs))))/((norm(rrs-mean(rrs)))*(norm(fffs-mean(fffs))))
  }
  return(cc)
}

the error says

Error in myopt(sol, op) : could not find function "off"

however

Upvotes: 1

Views: 134

Answers (3)

Zheyuan Li
Zheyuan Li

Reputation: 73385

fff is not a function but inside an if statement; what is wrong?

There is a bug here. You should change this line:

fff = fff(1:length(rr))

to

fff = fff[1:length(rr)]

Watch out the way for subsetting. Brackets () signals functions.

The code should not even go there because the ff is an empty data frame. Where is the problem?

First, ff is not an empty data frame, but an empty vector.

Second, the program will go to that line.

  if (length(ff) > 0){
    # then the step is 
    step <- (length(ff)+ inter)/(length(rr))
    fff <- interp1(ff,seq(1,length(ff), by=step))
  }  

The above block is if statement for length(ff) > 0. The call fff = fff[1:length(rr)] is outside this block, but inside if(length(fff) >= length(rr)). This evaluation is TRUE, because length(fff) = 0 and length(rr) = 0. If you really think the program should not reach this line, then your function has been incorrectly written.


Your new problem:

  ff <- c() # creates an empty df
  rr <- c() # creates an empty df
  fff <- c()  ## add this line

This part of code:

  if (length(ff) > 0){
    # then the step is 
    step <- (length(ff)+ inter)/(length(rr))
    fff <- interp1(ff,seq(1,length(ff), by=step))
  }

is conditional. If it is not run, then there is no fff variable defined. Then there will be trouble in the following:

if (length(fff) >= length(rr))

Upvotes: 2

Technophobe01
Technophobe01

Reputation: 8676

Nik,

The error is on line 16. fff = fff(1:length(rr)).

Your code is calling fff as a function. I hope that helps point you to the error.

Try trace(myopt(so,op))

thus,

 fff = fff[1:length(rr)]

Debugging R

Check out the following information on Debugging R this will hopefully be of use in future.

RStudio Debugging

Here is an example of how to debug in RStudio. Load the code, then in the console type trace(myopt(so,op))

now we see the error

> trace(myopt(so,op))
Error in myopt(so, op) : could not find function "fff"
Called from: myopt(so, op)
Browse[1]>

we make the change to the code...

fff = fff[1:length(rr)]

note that you have to reload the function, prior to running again, as the function definition has changed...

myopt <- function(so, op) {
+   # here I make two empty 
+   ff <- c() # creates an empty df
+   rr <- c() # creates an empty df
+   fff <-c()
+   inter = round(so[1]); # interpolation
+   sid = round(so[2]); # sideways movement
+   # if the lenght ff is bigger than 0
+   library(pracma)
+   if (length(ff) > 0){
+     # then the step is 
+     step <- (length(ff)+ inter)/(length(rr))
+     fff <- interp1(ff,seq(1,length(ff), by=step))
+   }  
+   if (length(fff) >= length(rr)){
+     fff = fff[1:length(rr)]
+   } else {
+     if (length(fff) < length(rr)){
+       diff = length(rr)-length(fff)
+       fff = c(fff,ones(1,diff) * ff(length(ff)))
+     }
+   }
+ }
> myopt(so, op)
> 

enter image description here

Upvotes: 2

rar
rar

Reputation: 924

Try fff[1:length(rr)] instead of fff(1:length(rr)) in if else statement.

Upvotes: 0

Related Questions