Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 从函数矩阵中获取函数行_Matlab_Function_Matrix - Fatal编程技术网

Matlab 从函数矩阵中获取函数行

Matlab 从函数矩阵中获取函数行,matlab,function,matrix,Matlab,Function,Matrix,在用我试图概括我的问题的各个方面的例子来压倒你之前,我将尝试尽可能简单地陈述问题: % 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 f

在用我试图概括我的问题的各个方面的例子来压倒你之前,我将尝试尽可能简单地陈述问题:

% 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,:)
如果f11,fnm是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)]
我如何通过迭代来解决这个问题?i、 例如:

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
% 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
下面是一个例子,说明了为了不遗漏我的实际问题的任何细节,我在这方面做了一些改进,但我已经尽可能地将其缩小:

% 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是返回stepsi x m矩阵的任何函数

我想知道小的具体示例和一般示例是否都应该通过一般迭代来解决,比如:

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
% 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

因此,技巧在于将函数定义为单元矩阵,然后使用一些向量化来解决问题。这是我想出的代码:

%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_垫