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

19 views (last 30 days)
tomer kapuri on 4 Dec 2018
Edited: Kevin Chng on 5 Dec 2018
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?
KSSV on 4 Dec 2018
(1.22+2.22+2.28)/3 = 1.90...
What do you mean by odd numbers here in the array?

Bruno Luong on 4 Dec 2018
Edited: Bruno Luong on 4 Dec 2018
a = [1.3700 1.2200 2.2000 2.2800];
ac = round(a*100);
mean(ac(mod(ac,2)==0))/100
result
ans =
1.9000
madhan ravi on 4 Dec 2018
Exactly!

Kevin Chng on 4 Dec 2018
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))
Kevin Chng on 4 Dec 2018
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 on 4 Dec 2018
Hi Tomer,
I am not sure if I understand your question correctly.
In MATLAB to calculate the average you would use the function: mean
a = [1.37 1.22 2.20 2.28];
yourAverage = mean(a)
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.