|
"li tai fang" <thecalbear@gmail.com> wrote in message
news:h6ej0g$7vr$1@fred.mathworks.com...
> Say, I have a function that takes 1 single input and generates 2 outputs:
> [x1, x2] = function(x);
>
> But then, I want to take the 2 outputs, and put them into the function:
> [x11, x12] = function(x1); [x21, x22] = function(x2);
>
> And once again, take the 4 outputs, and pass them through the same
> function yet again to generate 8 outputs, which is to be evaluated with
> the same function again, and so forth.
>
> Any idea how exactly do I do that in matlab?
Are all the outputs scalars of the same data type? If so, something along
these lines would work:
function y = recurse(fun, x, level)
if nargin < 3 || isempty(level)
level = 1;
end
y = zeros(1, 2*size(x, 2));
for k = 1:size(x, 2)
[y(2*k-1), y(2*k)] = fun(x(k));
end
if level > 0
y = recurse(fun, y, level-1);
end
If they're not, you'd need to do the same sort of thing, but with cell
arrays. Note: I haven't tried this code; if something is wrong with it or
if you want to extend it, feel free to do so at your own risk.
--
Steve Lord
slord@mathworks.com
|