problem in recursive function
显示 更早的评论
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 个评论
Image Analyst
2012-9-1
编辑:Image Analyst
2012-9-1
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
2012-9-1
编辑:Azzi Abdelmalek
2012-9-1
suppose you have to do it three times, what is the the expected result?
Jan
2012-9-1
The question is not clear. What do you want to divide?
Azzi Abdelmalek
2012-9-1
i think he means for two iterations
it1) [a,b]=divede(c);
it2) [a1,a2]=divede(a)
[a3,a4]=divede(b)
...
Walter Roberson
2012-9-1
Sounds like a recursive function rather than a recurrant function.
Sounds like a factoring problem.
huda nawaf
2012-9-1
huda nawaf
2012-9-2
编辑:huda nawaf
2012-9-2
采纳的回答
更多回答(1 个)
Azzi Abdelmalek
2012-9-1
编辑:Azzi Abdelmalek
2012-9-1
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 个评论
huda nawaf
2012-9-1
huda nawaf
2012-9-1
Azzi Abdelmalek
2012-9-1
编辑:Azzi Abdelmalek
2012-9-1
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
2012-9-1
can you post your first value c? you dd'nt mention that c is a vector
huda nawaf
2012-9-1
Azzi Abdelmalek
2012-9-1
编辑:Azzi Abdelmalek
2012-9-1
ok, check the updated code
huda nawaf
2012-9-1
Azzi Abdelmalek
2012-9-1
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
2012-9-2
Azzi Abdelmalek
2012-9-2
no , at the begening
s=[] and s1=[]
huda nawaf
2012-9-2
Azzi Abdelmalek
2012-9-2
编辑:Azzi Abdelmalek
2012-9-2
that 's what the code i've posted i guess is doing. did you try it? if yes what is the problem?
Azzi Abdelmalek
2012-9-3
编辑:Azzi Abdelmalek
2012-9-3
%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} % ...
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!