How to rewrite a conditional recursive funtion without for-loop

Hi, I have another problem with a recursive function that is a drag on performance (especially if m is a large number). The problem is a condition on the vector within the for-loop and that the values of the vector are added to x(i-1) before multiplying it. Here is a short example:
m=20;
n1=floor(rand(m,1).*10);
n2=floor(rand(m,1).*10);
n3=floor(rand(m,1).*10);
x=zeros(m,1);
starting_point=4;
for i=starting_point:size(x,1)
if n1(i)>0 && n2(i)>0 && n3(i)>0
x(i)=0.5*(n1(i)+n2(i)-n3(i)+x(i-1));
end
end
Again, any help is much appreciated!

Answers (3)

There is no recursion in the code you show. For there to be recursion there would need to be a function declaration that shows us that some routine is calling itself, and returning a value.
The code you show hints that perhaps you intended the recursion to be on a function named x, but if you were doing recursion on a function named x the only time you could assign indexed into x would be if you were defining a particular value of a symbolic function (which would require using the Symbolic Toolbox)
The code you show cannot, however, be the body for any recursive function. Recursion functions have the property that if N1 == N2 then f(N1) == f(N2), and that is a property that the given code violates because it uses random numbers in the calculation.
Your formula is at most iterative, not recursive.
Perhaps you were thinking of recurrence formulas rather than recursion . But recurrence formulas cannot have randomness in the coefficients either (they could, however, have randomness in the boundary conditions or in the constants used to define them, provided those constants were considered fixed for any one occurrence of the formula)

2 Comments

Hi Walter, Many thanks for your answer and sorry for probably using the wrong terminology. I called it "recursive" because I refer to x(i-1) to calculate x(i) (so a look-back in the vector that I am acually calculating). Also, I just used the random function to generate some values for n1, n2 and n3. However, these vectors are statig and not changing anymore within the for-loop. So to keep it simple: how do I calculate the vector "x" without a for-loop given that n1,n2,n3 are static vectors. Many thanks, Pascal
Is it correct that you want x(i) to be 0 (what you initialized to) unless n1(i)>0 && n2(i)>0 && n3(i)>0 is true?

Sign in to comment.

I don't think you can avoid the for loop because of your condition. The only optimisation that I can see is precalculating the condition and most operations non-dependent on x before the loop:
condition = n1 > 0 & n2 > 0 & n3 > 0;
nsums = n1 + n2 - n3;
for i=starting_point:size(x,1)
if condition(i)
x(i)=0.5*(nsums(i)+x(i-1));
end
end
I doubt it will have much of an impact though.
Hi Pascal!
Please only with for-end loop:
nn = [n1,n2,n3]; % nn = randi([0 10],20,3);
t = all(nn > 0,2);
nn(:,3) = -nn(:,3);
n = sum(nn,2);
n(~t) = 0;
n(1:3) = 0;
t(1:3) = false;
bw = bwlabel(t);
x = zeros(numel(n),1);
for jj = 1:max(bw)
tt = jj == bw;
x(tt) = filter(.5,[1,-.5],n(tt));
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 10 Sep 2016

Edited:

on 12 Sep 2016

Community Treasure Hunt

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

Start Hunting!