Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating a general function handle

  1. Aug 9, 2011 #1
    I'm writing a MATLAB function m-file which has input argument of some N-by-1 vector.

    Say this vector is [1,2,3].

    I want to create a function handle representing a sum of x taken to the power of each number that appears in the vector, so in this case it is:

    f = @(x) [x^1 + x^2 + x^3]; (x scalar)

    The point is, I don't know how to define f when I don't explicitly know what the input vector is. Can anyone help me generate a general function handle? The trick is because N is variable.
     
  2. jcsd
  3. Aug 9, 2011 #2

    uart

    User Avatar
    Science Advisor

    v=[1,2,3]

    f = @(x) sum(x.^v)
     
  4. Aug 10, 2011 #3
    I like it! Thanks very much
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?