How i Calculate the average number of even numbers in an array?

3 ビュー (過去 30 日間)
tomer kapuri
tomer kapuri 2018 年 12 月 4 日
編集済み: Kevin Chng 2018 年 12 月 5 日
How i Calculate the average number of even numbers in an array?
If the array:
1.3700 1.2200 2.2000 2.2800
so the avarage:
1.9
because: (1.22+2.22+2.28)/3=1.9
How i do this?
  1 件のコメント
KSSV
KSSV 2018 年 12 月 4 日
Check your question properly....
(1.22+2.22+2.28)/3 = 1.90...
What do you mean by odd numbers here in the array?

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

採用された回答

Bruno Luong
Bruno Luong 2018 年 12 月 4 日
編集済み: Bruno Luong 2018 年 12 月 4 日
a = [1.3700 1.2200 2.2000 2.2800];
ac = round(a*100);
mean(ac(mod(ac,2)==0))/100
result
ans =
1.9000
  3 件のコメント
Bruno Luong
Bruno Luong 2018 年 12 月 4 日
Here is what might happen if one does't take care about rounding
mod(1.12*100,2)==0
ans =
logical
0
madhan ravi
madhan ravi 2018 年 12 月 4 日
Exactly!

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

その他の回答 (2 件)

Kevin Chng
Kevin Chng 2018 年 12 月 4 日
My answer is
Check the index of Even number first
a = [1.3700 1.2200 2.2000 2.2800];
index = mod(single(a*100),2)==0;
Calculate the mean value of Even Number in the array
mean(a(index))
  5 件のコメント
tomer kapuri
tomer kapuri 2018 年 12 月 4 日
is not working...
Kevin Chng
Kevin Chng 2018 年 12 月 4 日
編集済み: Kevin Chng 2018 年 12 月 5 日
Ya, numel is calculating the number of element. Sorry for my mistake.
However, tomer kapuri, any difficulties you are facing since you say something is not working.

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


GT
GT 2018 年 12 月 4 日
Hi Tomer,
I am not sure if I understand your question correctly.
In MATLAB to calculate the average you would use the function: mean
In your example:
a = [1.37 1.22 2.20 2.28];
yourAverage = mean(a)
Please note in your question you asked about odd number. You could do:
mean(a(2:end))
This is adding the last 3 elements of the array a, and dividing by 3 as you illustrated, but there is something not right with your Maths... as:
(1.22+2.22+2.28)/3 is not 4.2.
Hope this helps.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by