How to exclude min and max only once in a if statement?

Can anyone please tell me how when using an if statement and taking the mean of a vector of any size. when I take the mean of the vector I am asked if the vector has more than 6 elements exclude the min and max. But if the min or max occurs more than once only exclude it once.
so far I have ...
function ave = improvedMean(V1)
n = length(V1)
if n>0 %%n<= 5
sum(V1) / n
elseif n >= 6
((sum(v1)-((min(v1)+(max(v1))) / (n-2)
I'm not sure where to go from there..

 採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 7 日

0 投票

What you did was correct. Just add an "end" at the end of your code and change
if n>0 %%n<= 5
to
if n>0 && n<= 5

その他の回答 (2 件)

the cyclist
the cyclist 2014 年 10 月 7 日
編集済み: the cyclist 2014 年 10 月 7 日

1 投票

function ave = improvedMean(V1)
n = length(V1)
if n <= 5
ave = sum(V1) / n
else
ave = (sum(V1)-(min(V1)+max(V1))) / (n-2)
end
José-Luis
José-Luis 2014 年 10 月 7 日
編集済み: José-Luis 2014 年 10 月 7 日

0 投票

Can be done with logical indexing:
Edited to take into account the six elements limit
a = randi(10,[5,1]);
n = size(a,1);
s = sort(a);
cond = diff(s);
idx = ones(n,1);
idx([1 end]) = ~cond([1 end]) .* n < 6;
your_mean = mean(s(logical(idx)));

4 件のコメント

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 7 日
But if the array is too large the sorting would be time consuming.
I think his approach is still better.
José-Luis
José-Luis 2014 年 10 月 7 日
I am not sure I follow. You still need to find out whether the min and max values are repeated, which none of the posted answers does.
Jason
Jason 2014 年 10 月 7 日
thanks!!
José-Luis
José-Luis 2014 年 10 月 7 日
Please accept the answer that best solves your problem.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 10 月 7 日

編集済み:

2014 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by