MATLAB: Bug with mean() on floating point vector elements

bugMATLABmeansum

Can someone explain this bug?
a = rand(100,1);
mean(a(4), a(6))
Output:
Error using sum
Dimension argument must be a positive integer scalar within indexing range.
Error in mean (line 117)
y = sum(x, dim, flag)/size(x,dim);

Best Answer

This is not a bug. Please read the documentation, if you have a problem with a command:
doc mean
The 2nd argument of mean must be the dimension to operate on. Then a floating point number between 0 and 1 will not work. See:
a = magic(3)
mean(a, 1)
mean(a, 2)
You forgot to mention, what you want to calculate. Maybe you mean:
a = rand(100, 1);
mean(a([4,6]))
% or
mean(a(4:6))