Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
Hello !
  3 comentarios
Peter Akortsu
Peter Akortsu el 24 de Sept. de 2018
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 el 24 de Sept. de 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))

Iniciar sesión para comentar.

Respuestas (1)

Joel Meyer Espinoza
Joel Meyer Espinoza el 24 de Sept. de 2018
Editada: Rik el 24 de Sept. de 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 comentarios
Joel Meyer Espinoza
Joel Meyer Espinoza el 24 de Sept. de 2018
i noticed thanks james
i am new to the forum
just trying to improve with answering questions
Rik
Rik el 24 de Sept. de 2018
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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by