am new to Matlab and can anyone help me with this question. Is there an alternative way of calculating the min and max of a vector without using functions min() and max()
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello !
3 Kommentare
Rik
am 24 Sep. 2018
As Joel outlined in his answer you need to think about what the min and max actually mean. What step-wise proces do you need to find the solution? There is a reason one of my professors told me he would rather give up his computer than his whiteboard when given a programming task: first design the process, then implement it in your language of choice.
An example of such a design might be this (this example will find the greatest common denominator). The design is written in comments, the code is a test to run.
%input: a, b (both integers saved as double)
a=2*3*19*31;b=2*17*31;
%first guess of the GCD is the smallest of the two
output=min(a,b);
%test if the guess is a denominator of both
if mod(a,output)==0 && mod(b,output)==0
else
%if not decrease the guess by one
output=output-1;
end
%repeat until the test returns true
while ~(mod(a,output)==0 && mod(b,output)==0)
output=output-1;
end
%show factors with built-in function to confirm:
clc,disp(factor(a)),disp(factor(b)),disp(factor(output))
Antworten (1)
Joel Meyer Espinoza
am 24 Sep. 2018
Bearbeitet: Rik
am 24 Sep. 2018
for max you could use this
a=[4 3 6 4 10 23 2 5 6]
max=a(1,1)
for i = 1:size(a,2)
if max < a(1,i)
max=a(1,i);
end
end
try to figure out min by yourself
it should not be difficult
4 Kommentare
Joel Meyer Espinoza
am 24 Sep. 2018
i noticed thanks james
i am new to the forum
just trying to improve with answering questions
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!