How to return the maximum value of a vector without using the built in function max(x)?

I have to calculate the maximum value of a vector, x, without using the built in function, only using basic mathematical,
relational, and logical operations. Then the maximum must be stored to a variable, y. The script must work with any vector assigned to x.

Réponses (2)

Apparently, you also cannot use a built-in sort algorithm. That leaves you the option of coding your own bubble-sort algorithm, or the Quicksort (link) algorithm.
K = rand(5) ;
themax = -Inf;
row = 0;
column = 0;
for i = 1:size(K, 1)
for j = 1:size(K, 2)
if K(i, j) > themax
themax = K(i, j);
row = i;
column = j;
end
end
end

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by