MATLAB: Recursive function to calculate Max number.

recursive function

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.
Please help with this code.

Best Answer

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
Related Question