0
votes

I am stuck at a problem related with cell array. I have a cell array X(size-400) with each element as a matrix with size (1x100). I want to get the average matrix so that I get the result as 1x100.I tried cellfun and other methods discussed previously. I am not able to get the desired result. Can I do it without converting the cell array into matrix.

2

2 Answers

1
votes

This is how I would do it: concatenate all the 1×100 vectors vertically into a matrix, then compute the mean of each column:

x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4
result = mean(vertcat(x{:}), 1); % concatenate vertically. Mean of each column

In this example,

result =
    7.3333    3.0000    5.0000    4.0000

If you really want to avoid the matrix, you can use a loop to accumulate the values:

x = {[9 4 5 4] [5 3 6 7] [8 2 4 1]}; % example cell array of 3 vectors of size 1×4
result = zeros(size(x{1})); % initiallize
for k = 1:numel(x)
    result = result + x{k}; % accumulate values
end
result = result / numel(x); % divide to obtain mean
0
votes

Were you using cellfun properly ? lets take an example of test cell which has 4 elements in each cell and has size 3

>> Cell1 = {[4 3 5 3], [1 2 3 4], [7 8 9 6]}

Cell1 = 

    [1x4 double]    [1x4 double]    [1x4 double]

Use an anonymous function x and then cellfun takes mean of each cell of x which in our case would be Cell1and then in the end we take the mean of the resulting matrix

  mean(cellfun(@(x)  mean(x),Cell1))

  ans =

       4.5833