www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SingletonExpansionwithCustomFunctionExample.m

    %% Expansion with Custom Function  
% Create a function handle that represents the function $f(a,b) = a - e^b$.
fun = @(a,b) a - exp(b);

%%
% Use |bsxfun| to apply the function to vectors |a| and |b|. The |bsxfun|
% function expands the vectors into matrices of the same size, which is an
% efficient way to evaluate |fun| for many combinations of the inputs.
a = 1:7;
b = pi*[0 1/4 1/3 1/2 2/3 3/4 1].';
C = bsxfun(fun,a,b)