0

I am currently working on a project in Matlab where I have a cell array of cell arrays. The first cell array is 464 columns long and 1 row deep. Each of these cells is another cell array that is 96 columns and 365 rows. I need to be able to get the mean of the 96 columns for each of the 464 arrays and place each of the 464 arrays on a different row in a new array called mean. I have tried to write code to just do one column as follow:

    mean = Homes{1,1}(1:)

But I when ever I try to run this code I got the follow error:

    mean = Homes{1,1}(1:)
                       |
    Error: Unbalanced or unexpected parenthesis or bracket.

Basically my final array name mean needs to be 96 columns by 464 rows. I am stuck and could really use your help.

Thank you.

  • `(1:)` definitely won't work, you can try `(1,:)`. I never know what to do with a cell array. I think if you put a loop through the top cell array and assign each cell to a temporary variable, you can definitely get a mean. – Dedek Mraz Mar 12 '13 at 20:49
  • Try using cellfun. You can try out cellfun(@mean,Homes) and this will apply the mean function to every element in the cell array. – JustinBlaber Mar 12 '13 at 21:58
  • When I tried cellfun(@mean,Homes) I recived this message... cellfun(@mean,Homes) Undefined function 'sum' for input arguments of type 'cell'. Error in mean (line 28) y = sum(x)/size(x,dim); – user2093732 Mar 12 '13 at 22:13
  • Now when I try. Mean = mean(Homes{1,1}(:,1)) I get the following errors. I am not sure why. Undefined function 'sum' for input arguments of type 'cell'. Error in mean (line 28) y = sum(x)/size(x,dim); Yet when I type Homes{1,1}(:,1) the first column is returned to me. – user2093732 Mar 12 '13 at 22:22

1 Answers1

0

I suggest you to try the following code on a smaller matrix. See if it gives you the desired results.

a=cell(1,4); %for you it will be a=cell(1,464)
for i=1:4
   a{i}=randi(10,[5 10]); %for you it will be a{i}=randi(10,[365 96]);
end
a1=cell2mat(a);  %just concatenating
a1=mean(a1); %getting the mean for each column. in your case, you should get the mean for 96*464
a2=reshape(a1,[10 4]); %now what reshape does it it takes first 10 elements and arranges it into first column.
%Therefore, since I finally want a 4x10 matrix (in your case 464x96), i.e. mean of 10 elements in first cell array on first row and so on...
%Thus, 1st 10 elements should go to first column after doing reshape (since you want to keep them together). Therefore, instead of directly making matrix as 4x10, first make it as 10x4 and then take transpose (which is the next step).

a2=a2'; %thus you get a 4x10 matrix.

In your case specifically, the code will be

a=cell(1,464); 
for i=1:464
   a{i}=randi(10,[365 96]); 
end
a1=cell2mat(a);  
a1=mean(a1); 
a2=reshape(a1,[96 365]);                        
a2=a2'; 
Autonomous
  • 8,935
  • 1
  • 38
  • 77