MATLAB: Wont it output index max function[max,index]=MyMax(x) max=0; [n,m]=size(x); for i=1:m if max<x(i) max=x(i); index=i; output = [max, index]; end end end Best Answer Hi,You have done everything correctly except for a couple of minor flaws. Here is the corrected code:function output = MyMax(x) MAX=0; % It is better to avoid MATLAB's function names, such as, max, min, etc to name variables [n, m]=size(x); for i=1:m if MAX<x(i) MAX=x(i); index=i; output = [MAX, index]; end end endThis can be tested:>> x=linspace(-pi,pi);>> OUT = MyMax(x)OUT = 1.0e+02 * 0.031415926535898 1.000000000000000 Note that the easy solution of your exercise is this simple command: [MAX_VAL, INDEX] = max(x)Good luck. Related SolutionsMATLAB: To find maximum value of any matrix without using built-in max() function [val,idx] = mymax(arr)val = arr(1);idx = 1; for k = 2:numel(arr) if arr(k)>val val = arr(k); idx = k; endendThis return the linear index, which is more useful than the subscript index. MATLAB: Is this not outputting anything For this to work you need to set your intial MIN_VALUE to inf rather than 0. If its set to zero, it will only make output if you send it a negative number, otherwise it will never go into that if statement.function output = MyMin(x)MIN_VALUE=inf;for i=1:length(x) if MIN_VALUE>x(i) MIN_VALUE=x(i); index=i; output = [MIN_VALUE, index]; endendend Related QuestionHow to find x for a given value of yHow to find the position of in the matrix without using the neither find nor built in functions.How to find multiple maximum values and they are indexes of 4-D arrayHow can i write 2D longest character arrayCentroid calculation for connected component in 3D volume
Best Answer