Is there a way to find the max value in an array without using the "max" command?

13 visualizzazioni (ultimi 30 giorni)
Cole Bromfield il 16 Gen 2020
Risposto: Walter Roberson il 16 Gen 2020
My professor wants us to create a function to find the max value in an array x (max=maxvalue(x)). He said we are not allowed to use the built-in max command. But what else is there?
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposte (4)

Cris LaPierre il 16 Gen 2020
Modificato: Cris LaPierre il 16 Gen 2020
The max function is just an implementation of an algorithm that compares the values in the vector to find the one with the highest value. You assignment, therefore, is to implement your own algorithm. I'd perhaps start with
if x(1) > x(2)...
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Murugan C il 16 Gen 2020
with the help of for and if, we can create a function to find max value in given array. If it is vector, use two for loop for row and col.
function max_Value = find_maxValue(Input)
max_Value = Input(1);
for i1 = 1 : length(Input)
if Input(i1) > max_Value
max_Value = Input(i1);
end
end
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Andrei Bobrov il 16 Gen 2020
B = A(:);
max_value = B(all(B - B' <= 0));
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Walter Roberson il 16 Gen 2020
sort() and take the endpoint.
Or loop comparing each entry to the largest so far and if the current entry is greater then update the running maximum.
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by