MATLAB: Removing max of a matrix

homework not originally tagged as homeworksubtract

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

Best Answer

Try this:
A=[23,43,32,45,76,56,34,65,92,57, 92, 92, 92, 76]
% Remove max(es) from A
A(A==max(A)) = []
% Determine the second max from A
secondhighestNumber = max(A)
Note, using == lets it remove more than one occurrence of the max, unlike if you had used max() to determine the index of the max.