How to delete two minimum elements in a vector?

25 ビュー (過去 30 日間)
Jiawei Weng
Jiawei Weng 2019 年 1 月 20 日
回答済み: Steven Lord 2019 年 1 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 20 日
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?
Image Analyst
Image Analyst 2019 年 1 月 20 日
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?

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2019 年 1 月 20 日
編集済み: madhan ravi 2019 年 1 月 20 日
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 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 20 日
Thank you sir Image Analyst edited the answer and sir Walter thank you for considering different situations.
Jiawei Weng
Jiawei Weng 2019 年 1 月 20 日
thank you so much

サインインしてコメントする。

その他の回答 (2 件)

Erik Keever
Erik Keever 2019 年 1 月 20 日
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 2019 年 1 月 20 日
Use the mink or maxk functions introduced in release R2017b.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by