MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by huda nawaf
on 1 Sep 2012

hi,

I have recurrent function , this function has two outputs and each output will be input next in time. I built divide function, this function divide the input into two parts , and each one of the two parts will be input to same function and get two outputs and so on

[a,b]=divede(c)

let divide whatever function

I need help to do that.

thanks

*No products are associated with this question.*

Answer by Image Analyst
on 2 Sep 2012

Accepted answer

Here is some recursive code:

% Demo to rotate an ellipse over an image. % By ImageAnalyst function [aboveMedian belowMedian] = test2(v) % Initialize aboveMedian = v; belowMedian = v; % Stopping condition. if length(v) <= 1 return; end medianValue = median(v); indexesAboveMedianVaue = v > medianValue; aboveMedian = v(indexesAboveMedianVaue); belowMedian = v(~indexesAboveMedianVaue); % Stopping conditions. if length(aboveMedian) <= 1 && ... length(belowMedian) <= 1 return; end if length(belowMedian) == length(v) return; end fprintf('For v = '); fprintf('%f, ', v); fprintf('\n Median = %f', medianValue); fprintf('\n Values above median = '); fprintf('%.1f, ', aboveMedian); fprintf('\n Values at or below median = '); fprintf('%.1f, ', belowMedian); fprintf('\n'); % Now recurse in with the two outputs test2(aboveMedian); test2(belowMedian);

It sort of does what you were trying to do with the median. Try it with this code:

a=[2 3 4 56 7 85 3 5 7 7]; [am bm] = test2(a)

But it will quit early because I'm not really sure when you want to stop. What are your stopping conditions when you won't recurse in anymore? I don't have the right stopping conditions in there. Anyway, it's a start and I'll leave it up to you to finish. Good luck.

Show 6 older comments

Image Analyst
on 3 Sep 2012

Well keep working at it. I'm sure with the debugger, you'll figure it out.

huda nawaf
on 4 Sep 2012

thanks image analyst, your code is very very help me to solve some problems in my code.

Now, I have another query related recursive function. I did modify for stop condition in above code:

Q_updat=(s1*B*s1')/2 if Q_updat<=0 return; end

now when Divide(G1) is running and test stop condition , and when met it will exit from code. How I can make the control of program go to Divide(G2),where stop condition stop the programm before raunning the secoind function. how solve this problem?

thanks

Image Analyst
on 4 Sep 2012

I don't know. Maybe just check the stop condition again or output a variable called "keepGoing" that you set to true or false and can check.

Answer by Azzi Abdelmalek
on 1 Sep 2012

Edited by Azzi Abdelmalek
on 1 Sep 2012

n=5;c={rand(1,100)}; for k=1:5 c1=[]; for i1=1:length(c) [a,b]=divede(c{i1}) c1=[c1 ;{a};{ b}]; end c=c1 end

% your results are

c{1},c{2} ,c{3} % ...

Show 10 older comments

huda nawaf
on 2 Sep 2012

ok , what if place s=[] and s1=[]?

how my problem of recurrent will be resolved? is not there way to make each output as input and give me each time two outputs and repeat the process?

Azzi Abdelmalek
on 2 Sep 2012

that 's what the code i've posted i guess is doing. did you try it? if yes what is the problem?

Azzi Abdelmalek
on 3 Sep 2012

%maby we are not using the same function dived; s and s1 should be initialized

function [p o]=dived(x) s=[];s1=[] k=1;k1=1; for i=1:length(x) if x(i)>median(x) s(k)=x(i); k=k+1; else s1(k1)=x(i); k1=k1+1; end end p=s; o=s1;

the code using dived

n=5;c={rand(1,100)}; for k=1:5 c1=[]; for i1=1:length(c) [a,b]=dived(c{i1}) c1=[c1 ;{a};{ b}]; end c=c1 end

% your results are

c{1},c{2} ,c{3} % ...

## 7 Comments

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97057

I've never heard of a recurrent function. Do you mean recursive? If it outputs two outputs, how can they be an input into a function that takes only one input??? I think you need to think about this a lot more.

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97064

suppose you have to do it three times, what is the the expected result?

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97066

The question is not clear. What do you want to divide?

## Azzi Abdelmalek (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97067

i think he means for two iterations

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97074

Sounds like a

recursivefunction rather than arecurrantfunction.Sounds like a factoring problem.

## huda nawaf (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97077

thanks,

let this function

what i need is,each of output will be the input to this function i.e [o p]=d(o) [o p]=d(p) each output will be input to this function and so on say do that for 5 times, in fact with my case i know the condition stop.

## huda nawaf (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/47182#comment_97159

yes, i mean recursive for image analyst I mean if I have

i have stop condition.