problem in recursive function
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 1 de Sept. de 2012
Editada: Image Analyst
el 1 de Sept. de 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
el 1 de Sept. de 2012
Editada: Azzi Abdelmalek
el 1 de Sept. de 2012
suppose you have to do it three times, what is the the expected result?
Jan
el 1 de Sept. de 2012
The question is not clear. What do you want to divide?
Azzi Abdelmalek
el 1 de Sept. de 2012
i think he means for two iterations
it1) [a,b]=divede(c);
it2) [a1,a2]=divede(a)
[a3,a4]=divede(b)
...
Walter Roberson
el 1 de Sept. de 2012
Sounds like a recursive function rather than a recurrant function.
Sounds like a factoring problem.
huda nawaf
el 1 de Sept. de 2012
huda nawaf
el 2 de Sept. de 2012
Editada: huda nawaf
el 2 de Sept. de 2012
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 1 de Sept. de 2012
Editada: Azzi Abdelmalek
el 1 de Sept. de 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 comentarios
huda nawaf
el 1 de Sept. de 2012
huda nawaf
el 1 de Sept. de 2012
Azzi Abdelmalek
el 1 de Sept. de 2012
Editada: Azzi Abdelmalek
el 1 de Sept. de 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
el 1 de Sept. de 2012
can you post your first value c? you dd'nt mention that c is a vector
huda nawaf
el 1 de Sept. de 2012
Azzi Abdelmalek
el 1 de Sept. de 2012
Editada: Azzi Abdelmalek
el 1 de Sept. de 2012
ok, check the updated code
huda nawaf
el 1 de Sept. de 2012
Azzi Abdelmalek
el 1 de Sept. de 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
el 2 de Sept. de 2012
Azzi Abdelmalek
el 2 de Sept. de 2012
no , at the begening
s=[] and s1=[]
huda nawaf
el 2 de Sept. de 2012
Azzi Abdelmalek
el 2 de Sept. de 2012
Editada: Azzi Abdelmalek
el 2 de Sept. de 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
el 3 de Sept. de 2012
Editada: Azzi Abdelmalek
el 3 de Sept. de 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} % ...
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!