Thread Subject: recursion with a function that generate 2 outputs

Subject: recursion with a function that generate 2 outputs

From: li tai fang

Date: 18 Aug, 2009 15:57:04

Message: 1 of 3

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?

Thanks in advance!

Li Tai

Subject: recursion with a function that generate 2 outputs

From: carlos lopez

Date: 18 Aug, 2009 16:06:03

Message: 2 of 3

"li tai fang" <thecalbear@gmail.com> wrote in message <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?
>
> Thanks in advance!
>
> Li Tai
Hello Li Tai:
Maybe you want to concatenate both outputs using Nan-separated format, like

newX=[x1;nan;x2]
and modify accordingly your function routine to dissasemble the input according to the nan-separator, perform the operation in each part and assemble the output as nan-separated values.
Notice that the size of newX will grow exponentially (2^n) while recursing, so beware of hitting the ceiling limits!
Regards
Carlos

Subject: recursion with a function that generate 2 outputs

From: Steven Lord

Date: 18 Aug, 2009 17:16:26

Message: 3 of 3


"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
recursion li tai fang 18 Aug, 2009 11:59:05
rssFeed for this Thread

Contact us at files@mathworks.com