Want to use a vector as parameter to a function, without having to separate its elements

前端 未结 7 693
猫巷女王i
猫巷女王i 2021-01-03 04:35

If I call a matlab function with: func(1,2,3,4,5) it works perfectly.

But if I do: a=[1,2,3,4,5] %(a[1;2;3;4;5

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 04:53

    Another method would be to create a separate inline function. Say you have a function f which takes multiple parameters:

    f = f(x1,x2,x3)
    

    You can call this with an array of parameter values by defining a separate function g:

    g = @(x) f(x(1),x(2),x(3))
    

    Now, if you have a vector of parameters values v = [1,2,3], you will be able to call f(v(1),v(2),v(3)) using g(v).

提交回复
热议问题