Find if max elements in array < or > a given number

2 ビュー (過去 30 日間)
Meg Cullen
Meg Cullen 2019 年 5 月 14 日
編集済み: madhan ravi 2019 年 5 月 14 日
I would like to write a code to calculate if a array, for example:
[6 7 4 8 9] has maximum elements above 5 (this number will be equal to number of elements) or not.
Here 4 out 5 elements are above 5 so the output will show "max elements greater than 5".
In case the array was [2 3 5 8 9] 3 elements are less than or equal to 5(output: "max elements less than 5" ).
Please help construct this code.
  2 件のコメント
Adam Danz
Adam Danz 2019 年 5 月 14 日
Are you trying to determine if most of the values in the vector are greater than or equal to 5? That's how I understood the description but your 2nd example doesn't follow that rule so I'm confused.
Meg Cullen
Meg Cullen 2019 年 5 月 14 日
編集済み: Meg Cullen 2019 年 5 月 14 日
@Adam_Danz its my mistake....I corrected it now. :)

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

採用された回答

Kevin Phung
Kevin Phung 2019 年 5 月 14 日
編集済み: madhan ravi 2019 年 5 月 14 日
a = [6 7 4 8 9];
val = numel(a);
if sum(a >= val) > val/2
disp(['Max elements greater than or equal to: ' num2str(val)]);
else
disp(['Max elements less than: ' num2str(val)]);
end
change the vector 'a' as you wish

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by