get the minimum and maximum element from two vectors.

44 Ansichten (letzte 30 Tage)
Shuoze Xu
Shuoze Xu am 8 Apr. 2022
Kommentiert: Shuoze Xu am 8 Apr. 2022
Hi.
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.
Testing
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;
end
end
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;
end
end
if n1 < n2
d1 = n1;
end
end
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.

Akzeptierte Antwort

Davide Masiello
Davide Masiello am 8 Apr. 2022
Bearbeitet: Davide Masiello am 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]);
end

Weitere Antworten (1)

Riccardo Scorretti
Riccardo Scorretti am 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).
  3 Kommentare
Riccardo Scorretti
Riccardo Scorretti am 8 Apr. 2022
Sure. inf = infinity. In your code, n1 = best candidate for the minimum. At the beginning of your algorithm, you should
  • either initialize n1 with v1(1) (assuming that v1 is not empty, otherwise you'll get an error) because min(v1) <= v1(1),
  • or n1 = inf because of course min(v1) <= inf.
If you initialize n1 = 0, you are implicitly assuming that min(v1) <= 0, which is likely to be wrong, and hence it will produce a wrong result. Even if you have specific a priori information on your data (= you are sure that min(v1) < 0), this is an extremely bad programming practice, and should be avoided in any case.
Shuoze Xu
Shuoze Xu am 8 Apr. 2022
I understand, and i have done that by using for loop.
Thanks for your help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by