Find the max of a vector recursive

40 views (last 30 days)
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))
if input(end-1) >= input(end)
input(end) = [];
input(end-1) = []

Accepted Answer

John D'Errico
John D'Errico on 13 May 2021
Edited: John D'Errico on 13 May 2021
This is a godawful problem to solve using recursive coding. Why? Suppose your vector has a million elements in it. Then this code will need to make 1 MILLION nested, recursive calls. That will be 1 MILLION separate function workspaces, all of which will be actiuve until the final call allows all of those recursive calls to unwind. 1 MILLION calls. Can you possibly want to do that? NO! Not in a million years.
Yes, I understand this is homework, and you need to do as directed. What I want you to understand is why that is such a bad idea for when you start writing code of your own.
Regardless, you made a (barely) credible start to your task, though your code is not really that close to being workable. What did you do wrong?
A recursive code will need to have an end condition. Your code has that, to deal with the problem when your vector has length 1. For longer vectors, You will compare the first element to recursive_max of the rest.
By the way, not needing to use other built-in functions means you cannot use max, but it also means you cannot even use the > comparison operator. That is in fact a built-in function. Anyway, your code should look most simply like that below:
x = [1 3 2 5 6 2];
ans = 6
function output = recursive_max(input)
if length(input) == 1
output = input;
output = recursive_max(input(2:end));
if input(1) > output
output = input(1);
So think about it. Will this work? Of course. Do you want to use that code in any rational form? YE GOD NO!
  1 Comment
Raymond Gilbers
Raymond Gilbers on 14 May 2021
Thanks John,
I appreciate your help. Although I think I understand what you have done. after the Else you give Output the vector input from 2 till end. and compare it with the first element input(1) if this is true output gets this value. It keeps doing this untill all elements are checked due to the statement recursive_max(input(2:end))
I get your point this would take up lots of memory when you use a huge vector. The main point of this exercise is to understand the recursive part of it. But it is tricky I think I understand your code but I would never come up with it.
Again thanks a lot

Sign in to comment.

More Answers (0)


Find more on Variables 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!