用matlab写一个求多个值中的最大值和最小值函数
时间: 2024-06-05 13:06:36 浏览: 57
function [max_val, min_val] = max_min_values(values)
% This function takes in an array of values and returns the maximum and minimum values.
% Initialize the maximum and minimum values to the first element of the array
max_val = values(1);
min_val = values(1);
% Loop through the remaining elements of the array
for i = 2:length(values)
% If the current value is greater than the maximum value, update the maximum value
if values(i) > max_val
max_val = values(i);
end
% If the current value is less than the minimum value, update the minimum value
if values(i) < min_val
min_val = values(i);
end
end
% Display the maximum and minimum values
disp(['The maximum value is ', num2str(max_val)]);
disp(['The minimum value is ', num2str(min_val)]);
end
% Example usage:
% values = [5, 8, 2, 10, 3, 7];
% [max_val, min_val] = max_min_values(values);
阅读全文