problem in recursive function

2 ビュー (過去 30 日間)
huda nawaf
huda nawaf 2012 年 9 月 1 日
コメント済み: farah fadel 2020 年 3 月 26 日
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 件のコメント
huda nawaf
huda nawaf 2012 年 9 月 1 日
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 2012 年 9 月 2 日
編集済み: huda nawaf 2012 年 9 月 2 日
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.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2012 年 9 月 2 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 26 日
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 2020 年 3 月 26 日
thank you, it worked

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
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 件のコメント
Azzi Abdelmalek
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
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} % ...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by