MATLAB: Count number of one values in vector in specific positions

countspecific values

I have a vector, x, and am trying to count the number of one values at each position every 3 rows.
x = [1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 ]'
In this case it should be 5 for the first row.
x = [1 0 0 1 0 0 0 1 0 1 0 0 0 1 0]'
In this case it should be 3 for the first row and 2 for the second row.
How can it get these answers out of it?

Best Answer

sum(reshape(x, 3, []), 2)
Example
x1 = [1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 ]';
y1 = sum(reshape(x1, 3, []), 2)
x2 = [1 0 0 1 0 0 0 1 0 1 0 0 0 1 0]';
y2 = sum(reshape(x2, 3, []), 2)
Result
>> y1
y1 =
5
0
0
>> y2
y2 =
3
2
0