Sunteți pe pagina 1din 2

Find the greatest and lowest value in the given

vector
using manual method using builtin function MAY 28, 2020

clc
close all
clear all
% input vector
% x = input('Enter the vector:');
x = [20, 5, 10, 1000, 2000, 199, -100, 201, 130];
M = length(x); % number of elements in the vector
vecMax = x(1); % Consider first value as maximum
vecMin = x(1); % Consider first value as minimum

% loop to find the maximum value


for i = 1:M
if x(i)>vecMax
vecMax = x(i);
end
end

% loop to find the minimum value


for i = 1:M
if x(i)<vecMin
vecMin = x(i);
end
end

fprintf('The maximum value in the given vector is:\n');


disp(vecMax); % Display maximum value result

vecMaxValue = max(x); % Using builtin function


fprintf('The maximum value using MATLAB builtin function:\n');
disp(vecMaxValue);

fprintf('The minimum value in the given vector is:\n');


disp(vecMin); % Display minimum value result

vecMinValue = min(x); % Using builtin function


fprintf('The minimum value using MATLAB builtin function:\n');
disp(vecMinValue);

The maximum value in the given vector is:


2000

The maximum value using MATLAB builtin function:


2000

The minimum value in the given vector is:


-100

1
The minimum value using MATLAB builtin function:
-100

Published with MATLAB® R2018a

S-ar putea să vă placă și