MATLAB: How would you calculate the average of odd indexed elements of a vector

loops

The code I have to get this answer is mean(V(1:2:end))
but I would I find it using at least one loop?

Best Answer

tot = sum(V(1:2:end));
N = 0;
for idx = 1:2:length(V)
N = N + 1;
end
tot/N
Related Question