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 (최근 30일)
이전 댓글 표시
Hello !
댓글 수: 3
Rik
2018년 9월 24일
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))
답변 (1개)
Joel Meyer Espinoza
2018년 9월 24일
편집: Rik
2018년 9월 24일
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
Joel Meyer Espinoza
2018년 9월 24일
i noticed thanks james
i am new to the forum
just trying to improve with answering questions
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!