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 comentarios

Image Analyst
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
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
Jan el 1 de Sept. de 2012
The question is not clear. What do you want to divide?
i think he means for two iterations
it1) [a,b]=divede(c);
it2) [a1,a2]=divede(a)
[a3,a4]=divede(b)
...
Walter Roberson
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
huda nawaf el 1 de Sept. de 2012
thanks,
let this function
function [p o]=d(x)
%%x any vector of values
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;
end
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
huda nawaf el 2 de Sept. de 2012
Editada: huda nawaf el 2 de Sept. de 2012
yes, i mean recursive for image analyst I mean if I have
while ()
[a b]=d(p)
[a1 a2]=d(a)
[b1 b2]=d(b)
[a11 a22]=d(a1)
[b11 b22]=d(b1)
and so on
end
i have stop condition.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 2 de Sept. de 2012

1 voto

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.

16 comentarios

huda nawaf
huda nawaf el 3 de Sept. de 2012
thanks Aziz,
The problem in second itr
*??? Attempted to access a(1); index out of bounds because numel(a)=0.
Error in ==> test2 at 4 a(1)
Error in ==> d at 22 [a,b]=test2(c{i1}) *
This test code: %%%this ish function%%%
function [c,d]=h(a)
b1=magic(7);
b=b1(1,:);
a(1)
if a(1)>1
c=a*b';
d=a+b;
else
c=a*b'
d=[];
end
end
%%%%this is your code
n=5;c={rand(1,7)};
for k=1:n
k
c1=[];
for i1=1:length(c)
[a,b]=d(c{i1})
c1=[c1 ;{a};{ b}];
end
c=c1
end
huda nawaf
huda nawaf el 3 de Sept. de 2012
thanks image analyst, I will try your code
huda nawaf
huda nawaf el 3 de Sept. de 2012
image analyst,
I do not understand this condition what it is working
if length(belowMedian) == length(v)
return;
end
thanks
Image Analyst
Image Analyst el 3 de Sept. de 2012
Just run it and see. Sometimes, like with my example, you'll get [7 7 7], and if you don't have that check, it will just do that over and over again because it tries to keep going until there's only one value left. But with 7 7 7 it will never stop unless you check for that. You might also use unique().
huda nawaf
huda nawaf el 3 de Sept. de 2012
Editada: huda nawaf el 3 de Sept. de 2012
Yes, I know now what is meaning. I did what u did regarding stop condition, why when ran this function , the code is closed and exit from matlab.
function [G1,G2]=Divide(B)
G1=B;
G2=B;
[p o]=size(B);
%%%compute eignvalue and eignvector
[U Beta]=eig(B);
%%convert the Beta matrix(eignvalues) into vector%%convert the Beta matrix(eignvalues) into vector
k=1;
for i=1:p
for j=1:o
if i==j
Beta1(k)=Beta(i,j);
k=k+1;
end
end
end
%%%sort the Beta1 vector
[Beta1 ind]=sort(Beta1,'descend');
if Beta1(1)>0
for j=1:o
if U(ind(1),j)>0
s1(j)=1;
else
s1(j)=-1;
end
end
v=s1*B*s1';
if v>=0
%%%divide the eignvector into two groups
k=1;k1=1;
for j=1:length(s1)
if s1(j)>0
for j1=1:o
GGrp_1(k,j1)=B(j,j1);
track(k)=j;
end
k=k+1;
else
for j2=1:o
GGrp_2(k1,j2)=B(j,j2);
track1(k1)=j;
end
k1=k1+1;
end
end
d=0;d1=0;
for i=1:length(track1)
GGrp_1(:,track1(i)-d)=[];
d=d+1;
end
[m n]=size(GGrp_1);
for i=1:m
f(i)=sum(GGrp_1(i,:));
end
for i=1:m
for j=1:n
if i~=j
B_updat(i,j)=GGrp_1(i,j);
else
B_updat(i,j)=GGrp_1(i,j)-f(i);
end
end
end
%
for i=1:length(track)
GGrp_2(:,track(i)-d1)=[];
d1=d1+1;
end
[m1 n1]=size(GGrp_2);
for i=1:m1
f1(i)=sum(GGrp_2(i,:));
end
for i=1:m1
for j=1:n1
if i~=j
B1_updat(i,j)=GGrp_2(i,j);
else
B1_updat(i,j)=GGrp_2(i,j)-f1(i);
end
end
end
G1=B_updat;
G2=B1_updat;
[m n]=size(G1);
[m1 n1]=size(G2);
if m==p | n==o
return;
end
if m1==p | n1==o
return;
end
end
else
G1=B;
G2=0;
end
Divide(G1)
Divide(G2)
end
thanks in advance
huda nawaf
huda nawaf el 3 de Sept. de 2012
this matrix what i try
sim=[0 70 40 -50 10;
70 0 20 -20 -20;
40 20 0 -10 -10;
-50 -20 -10 0 50;
10 -20 -10 50 0];
Image Analyst
Image Analyst el 3 de Sept. de 2012
Well keep working at it. I'm sure with the debugger, you'll figure it out.
huda nawaf
huda nawaf el 4 de Sept. de 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
Image Analyst el 4 de Sept. de 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.
farah fadel
farah fadel el 25 de Mzo. de 2020
Editada: farah fadel el 25 de Mzo. de 2020
Image Analyst, Is there a way to keep storing all th outputs of this recursion?
Image Analyst
Image Analyst el 25 de Mzo. de 2020
Yes, just accept the variable and stuff it into some array, like a global variable
global allResults
theseResults = func(); % Call your recursive function.
allResults = [allResults, theseResults];
or something like that. If you want, you can attach the array to the handles structure if you're using GUIDE.
handles.allResults = [handles.allResults, theseResults];
The handles structure (or app if using App Designer) is pretty much like a global variable.
farah fadel
farah fadel el 26 de Mzo. de 2020
These methods didn't work(It just stored one result) because the two outputs of the function is matrix with different dimensions, so I was thinking to use cell structure, but the implemntation is not working
global saved
if isempty(saved); saved = cell(0);
[out1, out2] = yourfunction(whateve)
saved(end+1) = {out1, out2};
farah fadel
farah fadel el 26 de Mzo. de 2020
Editada: farah fadel el 26 de Mzo. de 2020
It still stores just the first two outputs
Walter Roberson
Walter Roberson el 26 de Mzo. de 2020
How many outputs does the function have? You said it has two outputs, and now you are complaining that it only saves the first two outputs.
You would use the above code everywhere that you call your function, if you want to save all the outputs of the recursion.
farah fadel
farah fadel el 26 de Mzo. de 2020
thank you, it worked

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 1 de Sept. de 2012
Editada: Azzi Abdelmalek el 1 de Sept. de 2012

0 votos

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
huda nawaf el 1 de Sept. de 2012
thanks, i will try what u wrote to see if work with my case.
huda nawaf
huda nawaf el 1 de Sept. de 2012
but u collect a and b in c1. and in fact a, and b are independent . if c is vector, the out put will be two vectors a, and b
So, each one of outputs will be input to same function and will get new pair a, and b and so on like binary tree, each child will be parted into two parts.
Azzi Abdelmalek
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
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
huda nawaf el 1 de Sept. de 2012
can modify code to make first c as vector not one value?
Azzi Abdelmalek
Azzi Abdelmalek el 1 de Sept. de 2012
Editada: Azzi Abdelmalek el 1 de Sept. de 2012
ok, check the updated code
huda nawaf
huda nawaf el 1 de Sept. de 2012
my code is complex , i tried write code of modularity and community structure algorithm of newman.
the important part of algorithm is recurrent. In fact the input is array and the output are two arrays, but I simplfied the task . So, supposed the input is vector and the output two vectors.
like this code
function [p o]=d(x)
%%x any vector of values
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;
end
what i need is,each of output will be the input to this function i.e [o p]=d(o) and [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.
Azzi Abdelmalek
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
huda nawaf el 2 de Sept. de 2012
so just place p=[]; and o=[].
right?
no , at the begening
s=[] and s1=[]
huda nawaf
huda nawaf el 2 de Sept. de 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
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
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} % ...

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Sept. de 2012

Comentada:

el 26 de Mzo. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by