MATLAB: Find mean of rows containing decimal numbers in between integers in a column

mean

I have a column with mostly decimal numbers and some integers
Example (the integers are in bold). – Y = [1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1 4]. The integers range from 1 – 4.
I want to find the average of the numbers in between the integers, essentially [1 mean(0.098 0.00076 0.01) 2 mean (0.099 0.007) 2 mean(0.003 0.04 0.1) 4].
What would be the best way to do this? Is it recommended I turn the integers into a grouping variable?

Best Answer

Y = [1 0.098 0.00076 0.01 2 0.099 0.007 2 0.003 0.04 0.1 4];
Y=Y(:);
ix=diff(find(~mod(Y,1)))-1;
assert(nnz(~mod(Y,1))>2,'atleast one gap between integers is required')
y=Y(mod(Y,1)~=0);
V=mat2cell(y(1:sum(ix)),ix);
Wanted=cellfun(@mean,V)