function result = myMin(v) %myMin return the minimum value in a vector % % consumes: % v: a vector of numbers (row or column) % produces: % result: a scalar number from v, such that every element of v % is greater than or equal to result. % % examples: % % >> myMin([3 4 1 7 -2 9]) % ans = -2 % >> myMin([3 2 6 2 9]') % ans = 2 % %P. Conrad for CISC106 Fall 2006 % if the input is the empty vector, return an empty vector if length(v) < 1 result = []; return; end; % start with the assumption that v(1) is the minimum element min = v(1); % precondition: min is the smallest element in elements 1 thru 1 for k=2:length(v) % step through every element in the vector % invariant: min is smallest among elements 1 thru k-1 if (v(k) < min) % see if this element is smaller than min min = v(k); end; % end of if % invariant: min is smallest among all elements 1 thru k end; % end of for % post-condition: min is the minimum element in v % return the result result = min; return; end