[R] gradient

David Winsemius dwinsemius at comcast.net
Sat Mar 31 23:54:36 CEST 2012

On Mar 31, 2012, at 3:58 PM, mariam behboudi wrote:

> Hello
> In matlab we have "   gradient(F,h)   "  where h is a scalar uses h  
> as the
> spacing between points in each direction. Now I need to use this  
> function
> in R. and I dont know how can I should define my function that I  
> haave "h"
> in R?

I'm not a matlab user,  but perhaps if you define 'F' to be an n- 
column matrix and require 'h' to be of length one:

gradient <- function(F,h) { if( !is.matrix(F) | length(h) == 0 )  
{ stop() }
      return( apply(F ,2, diff)/h)  }

(The grad function in numDeriv takes a function as its first argument  
which is not what I thought you were asking for.)

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list