Recursive function with for loop inside?

조회 수: 9 (최근 30일)
Felipe Bayona
Felipe Bayona 2021년 3월 25일
편집: Jan 2021년 3월 25일
Hi Comunity.
I come asking for your wisdom
Im writing a recursive function that has a for loop inside.
The idea is to find a precise factor that must be inside the range [5, 10]
This factor is used to calculate the number of points inside a matrix that are above a threshold.
To achieve this I'm iterating over the range untill the number of elements over threshold is 1
For example, lets asume that the factor im looking for is 9.945
Iterating over 5 to 10 is not precise, so im calling the function recursively sending as arguments more precise limits, in this case 9 and 10
The function is working well: the correct factor is returned in the "recursive breaking condition", but due to the loop, continues in the first calls of the recursion, the result is overwriten.
Some ideas of how to achieve this correctly?
A simple idea would be to iterate from 5 to 10 whit a little step, like 0.00001, but the idea is to make it efficient. If the factor is inside the range [9, 10], previous values must be skipped quicly
Here's the code
temp is the matrix that contains numeric values.
Threshold is calculated with as 30 * factor
fact = factor(10, 5, 1, temp, 30);
function f = factor(upLim, downLim, step, temp, threshold)
for i = downLim : step: upLim
numNR = numel(temp(temp > threshold * i))
if step == 0.0001 % Return condition
f = i
return
elseif numNR == 1
f = factor(i, i-(step), step/10, temp, threshold); % Here the range is restricted and the step reduced
end
end
end
When finding the factor value = 9.945 with the step 0.0001, the function is returned, but in the previous calls the for loop continue iterating.
How to totally break the loop when intering the return condition?
Thx a lot for the ideas
  댓글 수: 5
Felipe Bayona
Felipe Bayona 2021년 3월 25일
Hi Jan
Temp is a 3D matrix with values from 1 to 1000. The idea is to find the threshold where there are only 1 value above.
For various statistical reassons, the threshold is one base number times a factor
B x Factor. In thios particular case B = 30, so the threshold must be a multiple of 30, where factor is between 5 and 10
Jan
Jan 2021년 3월 25일
편집: Jan 2021년 3월 25일
I assume Mathieu would insert the break after the "f = factor(..." line. Wouldn't this solve the problem?
But I still think that you should be able to solve this algebraic using the value of the threshold and the one or two largest elements of the array.

댓글을 달려면 로그인하십시오.

채택된 답변

David Hill
David Hill 2021년 3월 25일
I think you just need a break after your recurssive call.
function f = factor(upLim, downLim, step, temp, threshold)
for i = downLim : step: upLim
numNR = numel(temp(temp > threshold * i))
if step == 0.0001 % Return condition
f = i
return
elseif numNR == 1
f = factor(i, i-(step), step/10, temp, threshold); % Here the range is restricted and the step reduced
break;%I think you just need a break here
end
end
end
  댓글 수: 1
Felipe Bayona
Felipe Bayona 2021년 3월 25일
Thx David.
I was not sure how and instruction after the recursive call would be executed.
Thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by