Digital Signal Processing Reference
In-Depth Information
Note: MATLAB has functions min, max, mean (average), median, and sort
built-in. The questions below ask you to write your own code for these func-
tions, as if they were not built-in. The idea here is for you to get some
programming experience with MATLAB, with some easy-to-verify problems.
3. Write MATLAB code to give minimum and maximum values of x 1 and x 2 .
When you are happy with your code, make functions from it called \mymin"
and \mymax," respectively.
Answer:
%
% Mymax : return the maximum value of an array.
%
function max_val = mymax(x)
% Get an initial guess for the max
max_val = x(1);
% Now go through the rest of the array,
% 1 element at a time
for k=2:length(x)
% if the value at the current offset
% is bigger than our current max,
if (x(k) > max_val)
% ..then update our current max.
max_val = x(k);
end
end
% "max_val" will be returned automatically.
Now for the minimum-nding program.
%
% Mymin : return the minimum value of an array.
Search WWH ::




Custom Search