在用一些例子来压倒你之前,我试图尽可能简单地说明问题:
如果f11 , ... , fnm
是n*m
实值的功能,我希望通过n个步骤一次评估m,通过一些更高阶函数b,即
v = []
f1 = @(x) [f11(x) f12(x) ... f1m(x)]
v = [v b(f1)]
f2 = @(x) [f21(x) f22(x) ... f2m(x)]
v = [v b(f2)]
我如何通过迭代解决这个问题?就像这样:
f = @(x) [f11(x) ... f1m(x) ; ... ; fn1(x) ... fnm(x)];
% now iterate over the rows of f
for i=1:n
v = [v b(f(i,:)) ]
end
以下是我所拥有的一个例子(为了不错过我实际现实问题的任何细节而增长,但我试图让它尽可能小):
% 4 functions that take a 1x2 real valued vector as argument
% and return a real value
f11 = @(x) x(1) + x(2);
f12 = @(x) x(1) - x(2);
f21 = @(x) x(2) - x(1);
f22 = @(x) x(1) * x(2);
% we'll run function b for 2 steps, then 4 steps
steps = [2 4];
% start value
x = [1 2];
% vector to hold results
v = []
% get the result of passing the 1:st 2 functions to b with steps(1)
f1 = @(x) [f11(x) f12(x)];
v = [v ;b(x, f1, steps(1))]
% update x
x = v(end,:)
% add the result of passing the 2:nd 2 functions to b with steps(2)
f2 = @(x) [f21(x) f22(x)];
v = [v ;b(x, f2, steps(2))];
% update x
x = v(end,:)
其中b是定义如下的函数:
function [ X ] = b( x, f, n )
% @param:
% x = an 1x2 real valued vector
% f = a real valued function returning
% a 1x2 real valued vector
% n = an integer defining the rows of return matrix
%
% @return:
% X = an nx2 real valued matrix defined as below
X = zeros(n,2);
for i=1:n
% apply the functions on x
a = f(x+1);
b = f(x+2);
% update x
x = a+b
% add x to return matrix
X(i,:) = x;
end
end
上述代码可以概括为:
% n*m functions that take a 1xm real valued vector as argument
% and return a real value
f11 = @(x) ... ;
f12 = @(x) ... ;
.
.
.
fnm = @(x) ... ;
% we'll run function b for a1 steps, then a2 steps, ... , then an steps
steps = [a1 a2 ... an];
% start value
x = [1 2 ... m];
% vector to hold results
v = []
% get the result of passing the 1:st m functions to b with steps(1)
f1 = @(x) [f11(x) ... f1m(x)];
v = [v ;b(x, f1, steps(1))]
% update x
x = v(end,:)
% add the result of passing the 2:nd m functions to b with steps(2)
f2 = @(x) [f21(x) ... f2m(x)];
v = [v ;b(x, f2, steps(2))];
% update x
x = v(end,:)
.
.
.
% add the result of passing the n:ed m functions to b with steps(n)
fn = @(x) [fn1(x) ... fnm(x)];
v = [v ;b(x, fn, steps(n))];
% update x
x = v(end,:)
其中b是返回步骤(i)x m矩阵的任何函数。
我想知道小的具体例子和一般例子是否应该通过一般迭代来解决,如下所示:
% let f hold all the functions as a matrix
f = @(x) [f11(x) ... f1m(x) ; ... ; fn1(x) ... fnm(x)];
% now iterate over the rows of f
for i=1:n
v = [v ; b(x, f(i,:), steps(i)) ]
end
答案 0 :(得分:1)
所以诀窍在于将函数定义为单元矩阵,然后使用一些矢量化来解决问题。这是我提出的代码:
%First define your functions in a cell matrix
fn_mat = {@(x) x(1) + x(2), @(x) x(1) - x(2); ...
@(x) x(2) - x(1), @(x) x(1) * x(2)};
%Store the sixe of this matrix in two variables
[n, m] = size(fn_mat);
%Number of steps
steps = [2, 4];
% start value
x = [1 2];
% vector to hold results
v = [];
%This will run the required code for n iterations
for ii = 1:n
%This is the tricky part. What I have done is used arrayfun to run over
%all the functions in the row defined by ii and pass x as an argument
%each time. The rest is same as before
fn = @(x) arrayfun(@(a, b) fn_mat{ii, a}(b{:}), 1:m, repmat({x}, 1, m));
v = [v; b(x, fn, steps(ii))];
x = v(ii, :);
end
对于当前值,输出为:
v =
12 -2
26 28
-28 -13
30 610
1160 38525
74730 89497060
for循环足以适应任何大小的fn_mat。