Compute the mean of each vector in cell array C. C = {1:10, [2; 4; 6], []}; averages = cellfun(@mean, C) This code returns averages = 5.5000 4.0000 NaN
.............................................................
該函數就是專門對cell數組進行操作的,個人認為是代替了傳統的for循環,和C語言不一個思想,主要是行列化矩陣,一次處理。
比如a = {[1 2 3] [4 5 6]},那么cellfun(@length,a)得到6。線cell2mat也可以吧,然后分別計算,在累加。
Compute the size of each array in C, created in the previous example. [nrows, ncols] = cellfun(@size, C) This code returns nrows = 1 3 0 ncols = 10 1 0
..........................................................
cellfun中使用自定義函數對cell數組進行處理;既然可以自定義函數,那么cellfun中的的中間部分的參數個數和自定義函數需要的參數數目相同。
還能這么用。
queryNearestNeighborIds = cellfun( @(vector, index) vector(index), queryCandidateIds, queryNearestNeighborIds, 'UniformOutput', false );
................................................
Create a cell array that contains strings, and abbreviate those strings to the first three characters. Because the output strings are nonscalar, set UniformOutput to false. days = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'}; abbrev = cellfun(@(x) x(1:3), days, 'UniformOutput', false) The syntax @(x) creates an anonymous function. This code returns abbrev = 'Mon' 'Tue' 'Wed' 'Thu' 'Fri'