Borrar filtros
Borrar filtros

i am trying to write a function to find the second max number

1 visualización (últimos 30 días)
farah hattan
farah hattan el 12 de Jun. de 2018
Comentada: Jan el 12 de Jun. de 2018
can you edit this code so it can find the second max number
function [SeMax] = SecondMax(v)
n = numel(v);
res = sort(v);
for k = 1 : 1: n
res = v(1,end-1);
end
end
  5 comentarios
Walter Roberson
Walter Roberson el 12 de Jun. de 2018
Yes, I do know how to do any of those possibilities, but I do not know yet what the requirements are for your assignment.
Let us start with something simple: if the input is the single number 7, then what does the output have to be? Next, if the input is [7 7], then what does the output have to be? Next, if the input is [7 7 9], then what does the output have to be?
Jan
Jan el 12 de Jun. de 2018
Editada: Jan el 12 de Jun. de 2018
And what is wanted for [7 9 9]?
Maybe the unique command helps.

Iniciar sesión para comentar.

Respuestas (1)

Paridhi Yadav
Paridhi Yadav el 12 de Jun. de 2018
In your function you can write
res = sort(v)
SeMax = res(end-1)
It will give you the last second value after sorting, which is second largest if last and second last values are not same.
  1 comentario
Jan
Jan el 12 de Jun. de 2018
It is useful not to post solutions of homework questions. But you are suggesting only code, which was posted by the OP already. That's okay. +1

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by