get the minimum and maximum element from two vectors.

71 views (last 30 days)
That is question.
Write a MATLAB function called minMaxVectors that takes two vectors v1 and v2 (you can assume these vectors have the same length) as its parameters and returns two values: the minimum of both of these vectors, and the maximum of both of these vectors. You must also write a driver file called minMaxVectorsDriver to test your program.
Your driver program should embed test cases and print the results returned by minMaxVectors. Some example test cases are:
input1 input 2 min max
[ 1 2 ] [ 3 4 ] 1 4
[ 5 ] [ -5 ] -5 5
[ 1 9 ] [ 5 6 ] 1 9
That is my code
% That is function file
function [d1 d2] = minMaxVectors(v1,v2)
n1 = 0;
n2 = 0;
for i = 1:length(v1)
if n1 > v1(i) % if the number of min is bigger than v1
n1 = v1(i); % let min is equal to the elements of v1
d1 = n1;
for i = 1:length(v2)
if n2 < v2(i) % if the number of min is smaller than v2
n2 = v2(i); % let max is equal to the elements of v2
d2 = n2;
if n1 < n2
d1 = n1;
That is test file
% set two vector, first vector elements are 1 4,
% second vector elements are 4 -1
v1 = [1 4];
v2 = [4 -1];
[num1 num2] = minMaxVectors(v1,v2);
disp([num1 num2]);
On my code, it cannot be get the minimum element by comparing two vectors
The output is 0 4
How to improve my code?
Thank you all.

Accepted Answer

Davide Masiello
Davide Masiello on 8 Apr 2022
Edited: Davide Masiello on 8 Apr 2022
You could use the min and max MatLab inbuilt functions.
v1 = [1 4];
v2 = [4 -1];
[num1 num2] = minMaxVectors(v1,v2);
disp([num1 num2])
-1 4
function [minval maxval] = minMaxVectors(v1,v2)
minval = min([v1,v2]);
maxval = max([v1,v2]);

More Answers (1)

Riccardo Scorretti
Riccardo Scorretti on 8 Apr 2022
Dear Shouze, as your question looks like an assignment I'll provide only some hints, but I will not write any code in your place; I'm sure you will understand.
  1. To begin with, your algorithm has a weak point: you initializes n1 = n2 = 0. This is an error, which is likely to produce a wrong result: you should initialize them as n1 = inf and n2 = -inf if you want to obtain the correct result.
  2. Concerning names, when you read a program you must be able to tell which is the meaning of each variable (as much as possible, of course). In your case, the names n1 and n2 are meaningless: try to use something like minval instead of n1, and maxval instead of n2: your program will be much more readable.
  3. You could initialise n1 = n2 = v1(1) and start the first loop (and the first only!) from i = 2.
  4. Never use i and j as names of variables: it may be confounded with the imaginary unit 1i - expecially if you program in Matlab. Avoid also the variable l (= "letter L") because it can be easily misread as 1 (= "one") or | (= "or operator").
  5. You don't need the variables d1 and d2: just use n1 and n2.
  6. Last but not least, you wrote an algorithm in "C-style". In Matlab it would be much more effective to create a matrix the columns of which are the two vectors v1 and v2 (for instance by using mat = [v1(:) v2(:)]; the symbols (:) are required to ensure that v1(:) and v2(:) are column-vectors) then use the functions min and max (read the doc to know how).
Shuoze Xu
Shuoze Xu on 8 Apr 2022
I understand, and i have done that by using for loop.
Thanks for your help.

Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!