# recursive function to calculate Max number.

11 visualizzazioni (ultimi 30 giorni)
Urmish Haribhakti il 9 Set 2020
Risposto: Welid Benchouche il 18 Ago 2021
function mx=recursive_max(v)
if numel(v)==1
mx=v;
end
if numel(v)>1
mx= recursive_max(1);
end
this much i've got so far.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
JAYANTHI SANKARALINGAM il 20 Set 2020
When we use this,it shows error as
incorrectRandom vector Variable mx has an incorrect value. Test failed using v =[ 14 92 89 27 8 ]

Accedi per commentare.

### Risposta accettata

Stephen23 il 10 Set 2020
Modificato: Stephen23 il 10 Set 2020
Urmish Haribhakti's general approach is quite good, with a few small changes it will work correctly:
function x = recmax(v)
if numel(v)<=1
x = v;
else
x = recmax(v(2:end));
if v(1)>x
x = v(1);
end
end
end
And tested:
>> recmax([28,2,6,1,999,8])
ans = 999
##### 2 CommentiMostra NessunoNascondi Nessuno
Urmish Haribhakti il 10 Set 2020
Thank you so much!
Mohammed Shahin P il 14 Set 2020
can anyone please explain this code

Accedi per commentare.

### Più risposte (5)

Mati Somp il 9 Ott 2020
One more
function mx = recursive_max(v)
if length(v)==1
mx=v
else
if v(1)<=v(2)
v(1)=[]
else
v(2)=[]
end
mx=recursive_max(v)
end
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
xin yi leow il 25 Gen 2021
This works for me and is easy to understand! if v(1) < v(2), we remove the first element, and if v(2)<v(1), we remove the v(2), that way we only keep the larger of the 2 elements in the vector. The last remaining element will naturally be the largest one.

Accedi per commentare.

Ameer Hamza il 9 Set 2020
Modificato: Ameer Hamza il 10 Set 2020
Something like this:
function mx = recursive_max(v)
x = zeros(1, 2);
if numel(v) > 2
a = floor(numel(v)/2);
x(1) = recursive_max(v(1:a));
x(2) = recursive_max(v(a+1:end));
elseif numel(v) == 1
x = [v -inf];
else
x = v;
end
if x(1) > x(2)
mx = x(1);
else
mx = x(2);
end
end
I am not sure if this is an optimal algorithm.
##### 2 CommentiMostra NessunoNascondi Nessuno
Piyush Gupta il 10 Set 2020
Modificato: Piyush Gupta il 10 Set 2020
can you explain the code....
Ameer Hamza il 10 Set 2020
It divides the input vector into two parts recursively. When the length of the divided vector reaches two during recursion, the recursion stops, and the function returns back the largest of the two elements.

Accedi per commentare.

JAYANTHI SANKARALINGAM il 20 Set 2020
Write a function called recursive_max that finds the maximum element in a vector without using loop or any built in functions.the sole output argument is the maximum value in the input vector.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
JPS il 22 Dic 2020
this is your assignment question right?

Accedi per commentare.

Himalsha Dharmapala il 15 Gen 2021
function output= recursive_max3(v)
if v(1,:)==0
output=v(1);
else
if v(end)>=v
output=v(end);
else
output= recursive_max3(v(1:end-1));
end
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Welid Benchouche il 18 Ago 2021
my solution to this assignment is
function mx = my_recursive_max(v)
% base case
if length(v) == 1
mx = v;
else
% recursive case
a = v(end);
b = v(1);
if a <= b
mx = b;
mx = my_recursive_max(v(1:end-1))
else
mx = a;
mx = my_recursive_max(v(2:end))
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by