2016-03-18 97 views
1

多個變量我試圖做類似語法用於評估功能

f = [x+1 y+2] 
values = [1 2] 
f(values) = [2 4] 

(不正確的語法)

f(values)只能用於採取一個變量?

+1

[解釋在手冊中(http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html) – Daniel

回答

1

嘗試這種情況:

f = {@(x) (x+1); @(y) (y+2)}; %//create a cell array of your function handlers 
values = [1 2]; 

%//convert your input values to a cell array 
length = numel(values); 
v = mat2cell(values, 1, ones(length,1)).' ; 

%// f(v) 
results = cellfun(@(x,y) x(y), f, v);