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!