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

前端 未结 7 689
猫巷女王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:52

    Maybe you could try with nargin - a variable in a function that has the value of the number of input arguments. Since you have a need for different length input, I believe this can best be handled with varargin, which can be set as the last input variable and will then group together all the extra input arguments..

    function result = func(varargin)
        if nargin == 5: % this is every element separately
            x1 = varargin{1}
            x2 = varargin{2}
            x3 = varargin{3}
            x4 = varargin{4}
            x5 = varargin{5}
        else if nargin == 1: % and one vectorized input
            [x1 x2 x3 x4 x5] = varargin{1}
    

    I've written x1...x5 for your input variables

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-03 04:54

    Comma-seperated lists (CSL) can be passed to functions as parameter list,

    so what you need is a CSL as 1,2,3,4,5 constructed from an array.

    It can be generated using cell array like this:

    a=[1,2,3,4,5];
    c = num2cell(a);
    func(c{:});
    
    0 讨论(0)
  • 2021-01-03 04:54

    Since arguments to functions in Matlab can themselves be vectoes (or even matrices) you cannot replace several arguments with a single vector.
    If func expects 5 arguments, you cannot pass a single vector and expect matlab to understand that all five arguments are elements in the vector. How can Matlab tell the difference between this case and a case where the first argument is a 5-vector?

    So, I would suggest this solution

    s.type = '()';
    s.subs = {1:5};
    func( subsref( num2cell( otherfunc(b) ), s ) )
    

    I'm not sure if this works (I don't have matlab here), but the rationale is to convert the 5-vector a (the output of otherfunc(b)) into a cell array and then expand it as 5 different arguments to func.
    Please not the difference between a{:} and a(:) in this subsref.

    0 讨论(0)
  • 2021-01-03 04:56

    Use eval:

    astr = [];
    for i=1:length(a)
        astr     = [astr,'a(',num2str(i),'),']; % a(1),a(2),...
    end
    astr  = astr(1:end-1);
    eval(['func(' astr ');']);
    
    0 讨论(0)
  • 2021-01-03 05:04

    You could create a function of the following form:

    function [ out ] = funeval( f, x )
       string = 'f(';
       for I = 1:length(x)
          string = strcat( string, 'x(' , num2str(I), '),' );
       end
       string( end ) = ')';
       out = eval( string );
    end
    

    In which case, funeval( func, a ) gives the required output.

    0 讨论(0)
提交回复
热议问题