removing max of a matrix
Afficher commentaires plus anciens
Hello
I have a matrix like this
if true
A=[23,43,32,45,76,56,34,65,92,57];
end
now I want to find max and second max of it so after finding max of it, I want to remove it from matrix and then find another max (which would be second max) but I don't know how remove the first one. I use this:
if true
B=A - [x];
end
but it subtract first max from all of matrix elements
Réponse acceptée
Plus de réponses (1)
Star Strider
le 3 Juin 2017
This will remove the two largest elements of ‘A’ and leave the original order unchanged:
[As,idx] = sort(A,'descend');
A(idx([1 2])) = [];
A =
23 43 32 45 56 34 65 57
4 commentaires
M.S.A.A
le 3 Juin 2017
Star Strider
le 3 Juin 2017
My pleasure.
You did not specify that constraint in your original Question. You also did not mention that this was a homework problem. (We do not provide complete Answers to homework problems.)
Since I already posted a solution, this loop will produce the same result:
B = A;
for k1 = 1:2
[~,idx] = max(B);
B(idx) = [];
end
B % Display Result
The result is the same as previously posted.
Image Analyst
le 3 Juin 2017
MSAA asked to " find another max", not remove it. He also said to "remove the first one" and did not mention removing the second one. So that's why my solution removed any and all max values and found, but did not remove, the second highest value. My solution uses max(), not sort(). Please specify if max() is also not allowed.
M.S.A.A
le 3 Juin 2017
Catégories
En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!