Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to average over a cell-array of arrays?

I have a cell array c of equal-sized arrays, i.e. size(c{n}) = [ m l ... ] for any n. How can I get the mean values (averaging over the cell array index n) for all array elements in one sweep? I thought about using cell2mat and mean but the former does not add another dimension but changes l to l*n. And looping manually of course takes like forever...

like image 450
Tobias Kienzler Avatar asked Mar 04 '11 18:03

Tobias Kienzler


4 Answers

If all of your arrays are the same size, it makes more sense to store them in a matrix rather than a cell array. That makes it easier to perform operations across them, like taking the mean. You can convert your data to a matrix using the functions NDIMS and CAT:

dim = ndims(c{1});          %# Get the number of dimensions for your arrays
M = cat(dim+1,c{:});        %# Convert to a (dim+1)-dimensional matrix
meanArray = mean(M,dim+1);  %# Get the mean across arrays
like image 182
gnovice Avatar answered Nov 17 '22 08:11

gnovice


If you have a Higher version of matlab, It can be done by 'cellfun' function. This can treat the cells with unequal size array.

C = {1:10, [2; 4; 6], []};
Cmeans = cellfun(@mean, C)
Cmeans =
    5.5000    4.0000       NaN

Reference: https://groups.google.com/forum/?fromgroups=#!topic/comp.soft-sys.matlab/S_hyHxy11f0

like image 45
Victor Bai Avatar answered Nov 17 '22 08:11

Victor Bai


You're on the right track. Use CELL2MAT to convert your cell array to a numerical array and then RESHAPE to construct a three dimensional matrix. You can then calculate the mean using the MEAN function with the dimension argument:

>> c = {[1 2 3; 4 5 6] [7 8 9; 12 13 14]}

c = 

    [2x3 double]    [2x3 double]

>> mean(reshape(cell2mat(c), [2, 3, 2]), 3)

ans =

     4     5     6
     8     9    10
like image 2
b3. Avatar answered Nov 17 '22 10:11

b3.


I found an easy way to find the mean values within a Cell array on the following link: http://www.gomatlab.de/cellfun-t25114.html

May x be the cell. Then:

var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value


var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"
like image 2
Fried Rice Avatar answered Nov 17 '22 08:11

Fried Rice