Find the max of a vector recursive
Show older comments
Hello all,
Im trying to find the max value of an element in a vector, for learning purposes Ineed to do this recursive. I have been trying this all over again but something goes terribly worng. Could anybody point out to me what I am doing wrong? Thanks in advance.
function output = recursive_max(input)
%%Write a function called recursive_max that finds the maximum element in a
%%vector. You are not allowed to use loops or any built-in functions other
%%than length. The sole output argument is the maximum value in the input
%%vector. Hint: the maximum value of a vector is the larger of its first
%%element and the maximum of the rest of the elements.
l = length(input);
if l == 1
output = recursive_max(input(end))
else
if input(end-1) >= input(end)
input(end) = [];
recursive_max(input);
end
input(end-1) = []
end
end
Accepted Answer
More Answers (1)
Syed Muhammad Anas Ibrahim
on 12 Dec 2024
Edited: Syed Muhammad Anas Ibrahim
on 19 Dec 2024
Another compact solution can be as follows:
Thanks for carefull watch in code. Hope it could address nan issue as well
function val = maxi_func(n)
len = length(n);
if len==1
val = n(len);
elseif len==2
val = n(2);
if n(1)>n(2)||isnan(n(1))
val = n(1);
end
else
val = maxi_func(n(1:len-1));
if n(len)>val||isnan(n(len))
val = n(len);
end
end
end
2 Comments
Walter Roberson
on 13 Dec 2024
Edited: Walter Roberson
on 13 Dec 2024
That code can fail if there are inf or -inf .
Suppose the input is [inf inf] . Then
val = n(1)*(n(1)>n(2))+n(2)*(n(1)<n(2));
will be
val = inf*(inf>inf)+inf*(inf<inf);
The inf>inf part and the inf<inf part both yield 0. inf*0 is nan. So you would have inf*0+inf*0 which would be nan+nan which would yield nan.
The code also can fail if there are two identical finite values. Suppose the input is [3 3]. Then
val = n(1)*(n(1)>n(2))+n(2)*(n(1)<n(2));
would be
val = 3*(3>3)+3*(3<3);
The 3>3 part and the 3<3 part are both false, value 0, So it would be
val = 3*0 + 3*0
which is going to give 0. In this case of identical all-finite values, the val that is output is going to be 0 instead of the value.
The code has infinite recursion in the case that the input vector is a scalar.
Walter Roberson
on 18 Dec 2024
The adjusted code acts a bit oddly on NaN.
elseif len==2
val = n(2);
if n(1)>n(2)
val = n(1);
end
Suppose n(1) is nan and n(2) is finite. Then n(1)>n(2) is false and val=n(2) is still in effect, so you return the finite value.
Suppose n(1) is finite and n(2) is nan. Then n(1)>n(2) is false and val=n(2) is still in effect, so you return the nan.
Your code should handle nan consistently. Either it should ignore all nan and return the maximum of the finite elements, or else it should return nan if there is any nan. Whether it returns nan or not should not be up to the chance permutation of the elements.
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!