Try to find the maximum value in random matrix by comparing one element to another

5 vues (au cours des 30 derniers jours)
Merlyn Inova
Merlyn Inova le 26 Mai 2020
Modifié(e) : John D'Errico le 26 Mai 2020
Hello, I want to ask for help. I have a 1 x 10 random matrix . I want to find the largest value in the matrix by comparing the first to the last element. if the second element is greater than the first element, then the largest value changes from the first element to the second. but if not, the greatest value will remain in the first element. the comparison is continued until the last element. What should I add to the code below?
I have one more question, because this is a random matrix so every time I run the code the result is different. I want to compare the new result of the maximum value with previous result. If the new result is less than the previous result I want to keep maximum value from the previous running in maxval variable. How to do it? Thank you very much and sorry for my lack of English.
mat=rand(1,10);
for i = 1
if i
maxval=max(mat)
end
end
  2 commentaires
Rik
Rik le 26 Mai 2020
Why do you want to use a loop to find the maximum value? Is this homework?
John D'Errico
John D'Errico le 26 Mai 2020
Modifié(e) : John D'Errico le 26 Mai 2020
You already seem to be using max here. So why are you asking for help on how to find the maximum? max does it, and needs nothing more. No loop is even needed.
Are you asking how to find a cumulative maximum? If so, then just use cummax.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by