MATLAB: Apply cellfun for specified rows of each cell

cellfunmean

Hello
I have a cell containing double matrix of each row as example:
a={[1;2;4;5;2;3];[3;2;1;4;5;9;4;1;2];[];[2;3;4];[1;2;4;5;6]}
I want to apply CELLFUN at each cell for specified rows of each matrix for example row number from 2:4 of first matrix and rows from 3:6 of second and….
I wrote a loop for this but wonder if there would be easier way by using cellfun(@mean a) for specified row numbers of each matrix of a, do you know any?

Best Answer

rows is a cell array the same size and shape at a containing vectors of row numbers.
a={[1;2;4;5;2;3];[3;2;1;4;5;9;4;1;2];[];[2;3;4];[1;2;4;5;6]}
rows = {2:4, 3:6, [], 1:2, 2:4}';
mu = cellfun(@(x,i)mean(x(i)), a, rows);
Loop method (suggested by Image Analyst)
>2x faster than cellfun.
mu = nan(size(a));
for i = 1:numel(a)
mu(i) = mean(a{i}(rows{i}));
end