MATLAB

2011-04-14 30 views
17

'daki işlevlerin bir vektörü üzerinde yineleme MATLAB içindeki işlevlerin bir listesi üzerinde yineleme yapılabilir mi? Farklı radyal temel fonksiyonlarını test etmeye çalışıyorum ve bunu yapmanın en iyi yolu gibi görünüyor.MATLAB

cevap

22

cell array'un function handles numarasını yapabilir ve üzerinde yineleyin. Örneğin:

vec = 1:5;      %# A sample vector of values 
fcnList = {@max, @min, @mean}; %# Functions to apply to the vector 
nFcns = numel(fcnList);   %# Number of functions to evaluate 
result = zeros(1,nFcns);  %# Variable to store the results 
for iFcn = 1:nFcns 
    result(iFcn) = fcnList{iFcn}(vec); %# Get the handle and evaluate it 
end 
8

bunu size gnovice cevabı itibaren takip yapabilirsiniz çıkıyor kendi işlevlerini tanımlamak isterseniz: /:

funcList = {@(x, y) (x - y), @(x, y) (x + y)} 
+1

Yup, [anonim fonksiyonlar] (http için çalışıyor /www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html) de! – gnovice