How to return the maximum value of a vector without using the built in function max(x)?
Show older comments
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.
Answers (2)
Star Strider
on 30 Apr 2019
0 votes
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.
KSSV
on 30 Apr 2019
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
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!