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 个评论
Peter Akortsu
Peter Akortsu 2018-9-24
Hi Rik, thanks for the reply. I have no idea as to what code to write if am not to use min or max functions. Am totally new to programming and MATLAB. I have read the documentation on how to use min and max but the instruction from the homework makes it clear not to use then min and max functions. I don't want the easiest way out that is why I have not pasted the question. I just want to find out if there is a little bit complicated way I can think about finding min or max of a set of numbers without using the inbuilt functions.
Rik
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
编辑: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 个评论
Rik
Rik 2018-9-24
If you click the edit button next to your answer you can see what I've done. See here for a GIF showing how to format your code on this forum. The changes to the content (i.e. changing the variable name to something different) is something I will leave to you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by