problem in recursive function
Show older comments
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
7 Comments
Image Analyst
on 1 Sep 2012
Edited: Image Analyst
on 1 Sep 2012
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
on 1 Sep 2012
Edited: Azzi Abdelmalek
on 1 Sep 2012
suppose you have to do it three times, what is the the expected result?
Jan
on 1 Sep 2012
The question is not clear. What do you want to divide?
Azzi Abdelmalek
on 1 Sep 2012
i think he means for two iterations
it1) [a,b]=divede(c);
it2) [a1,a2]=divede(a)
[a3,a4]=divede(b)
...
Walter Roberson
on 1 Sep 2012
Sounds like a recursive function rather than a recurrant function.
Sounds like a factoring problem.
huda nawaf
on 1 Sep 2012
huda nawaf
on 2 Sep 2012
Edited: huda nawaf
on 2 Sep 2012
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 1 Sep 2012
Edited: 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} % ...
13 Comments
huda nawaf
on 1 Sep 2012
huda nawaf
on 1 Sep 2012
Azzi Abdelmalek
on 1 Sep 2012
Edited: Azzi Abdelmalek
on 1 Sep 2012
no, i 'am using one at time, c1(1), c1(2),...
c=[a b];
[a,b]=divede(c(1))
c1=[a b];
[a,b]=divede(c(2))
c1=[c1 a b]
% for the next iteration c=c1 contains 4 ellements
% at the end the vector c contain all your 2^n rsults
Azzi Abdelmalek
on 1 Sep 2012
can you post your first value c? you dd'nt mention that c is a vector
huda nawaf
on 1 Sep 2012
Azzi Abdelmalek
on 1 Sep 2012
Edited: Azzi Abdelmalek
on 1 Sep 2012
ok, check the updated code
huda nawaf
on 1 Sep 2012
Azzi Abdelmalek
on 1 Sep 2012
there is a problem in your funcion, when the if is skipped, p=s causes an error because s is not calculated, maybe yo need to initialize it
huda nawaf
on 2 Sep 2012
Azzi Abdelmalek
on 2 Sep 2012
no , at the begening
s=[] and s1=[]
huda nawaf
on 2 Sep 2012
Azzi Abdelmalek
on 2 Sep 2012
Edited: 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
Edited: 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} % ...
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!