How to delete two minimum elements in a vector?

wonder how to delete two minimum elements in the vector and then calculate the mean value of new vector
the vector is like A=[ 70, 56 , 30, 10 , 83 , 78, 77, 90]
code should be flexibe enough to calculate for any vectors

2 comentarios

Suppose A had three values that were all (say) 9. All 3 of them are the minimum. What do you want to do in that situation?
Your request is very imprecise.
What would you return for
v = [1 1 1 2 2 2 3]
??? Would you return
  1. [1 2 2 2 3] (removing a pair of 1's), or
  2. [3] (removing all the 1's and all the 2's?

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 20 de En. de 2019
Editada: madhan ravi el 20 de En. de 2019
EDITED
n=2; % two smallest values
u=unique(A);
if numel(u)==1 || numel(u)==2
Result=[]
else
Result=mean(A(~ismember(A,u(1:n))))
end

6 comentarios

This would give index out of range if all of the elements of the array are the same.
madhan ravi
madhan ravi el 20 de En. de 2019
Edited the answer according to that situation sir Walter.
Image Analyst
Image Analyst el 20 de En. de 2019
Why are you sorting A to produce temporary variable a???
unique() already returns the values sorted so there is no need to do it in advance.
Typically when there are duplicates, people choose from one of several different strategies:
  1. Use the count (two) as the number of unique values to remove, removing all copies of the minimum and all copies of the second lowest
  2. Start counting the number of items removed so far. If the count (two) has not been reached yet, remove all copies of the lowest value (incrementing the counter by the number removed), and then loop back. For example the array [A B B B C D E F] would start at 0 removed, remove all 1 copy of A incrementing the removed count to 1, then not having reached the target would remove all 3 copies of B, incrementing the removed count to 4, and then having reached the target of 2 removals, would end
  3. Start counting the number of items removed so far. If the count (two) has not been reached yet, count how many copies exist of the lowest value. If that number together with the number removed so far would exceed the target, then stop without further removal. So [A B B B C D E F] would become [B B B C D E F] and would stop early because the 3 B's would overflow the target of two removals,
  4. Start counting the number of items removed so far. If the count (two) has not been reached yet, find all of the copies of the lowest, and remove at most (target minus count) of them. Which you remove is another decision, and it matters if you are preserving the order of things not being removed
madhan ravi
madhan ravi el 20 de En. de 2019
Thank you sir Image Analyst edited the answer and sir Walter thank you for considering different situations.
Jiawei Weng
Jiawei Weng el 20 de En. de 2019
thank you so much

Iniciar sesión para comentar.

Más respuestas (2)

Erik Keever
Erik Keever el 20 de En. de 2019

0 votos

Hmm. Perhaps,
% generate some random junk
x = round(10*rand([24 1]));
% sort array
[~, idx] = sort(x);
% sort the indices of the (N-2) largest elements, cutting the 2 smallest out and select subset
xTrimmed = x(sort(idx(3:end)));
I've taken that you mean to remove precisely two elements which are lexcographically smallest. My code breaks the degeneracy among multiple smallest values by choosing the first ones to occur in the array, such that among [4 1 1 0 4 4 4], the 0 and the first 1 will be removed. For large arrays, avoiding my lazy second sort() by building the subsets explicitly would be faster (removing one of two N log N sort procedures).
Steven Lord
Steven Lord el 20 de En. de 2019

0 votos

Use the mink or maxk functions introduced in release R2017b.

Categorías

Productos

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 20 de En. de 2019

Respondida:

el 20 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by