MATLAB: To find maximum value of any matrix without using built-in max()

for looploopMATLABpracticequestion

function [max_value, index] = mymax(x)
[r,c]=size(x);
max_value_row=x(1,1)
max_value_col=x(1,1)
for i=1:c
if x(1,i)>max_value_row
max_value_row=x(i,1);
end
for k=1:r
if x(k,1)>max_value_col
max_value_col=x(k,1);
end
end
end
if max_value_row > max_value_col
max_value = max_value_row
else
max_value = max_value_col
index = find(max_value)
end
THis is what i have tried so far but the value found is wrong, please help me

Best Answer

function [val,idx] = mymax(arr)
val = arr(1);
idx = 1;
for k = 2:numel(arr)
if arr(k)>val
val = arr(k);
idx = k;
end
end
This return the linear index, which is more useful than the subscript index.