Get the avarage for coresponding cell contents from a cell array

1 view (last 30 days)
Hi,
I have two cell arrays.
  1. data = 1x175 cell (rows double array is not consistent for each cell. There are some cells which do not even have values.)
  2. Z1cell = 1x278 cell (each cell has a double array of 5479 rows)
I need to go through each cell of "data" and get the values for each cell, for example, data{1,1} has 272, 273,274. Then I need to get the corresponding cell from Z1cell, in this case, cell 272,273 and 274, and get the average for each row.
Hope I explained clearly.
Please see the two .mat files (data and Z1ccell) are attached.
Sample code I was trying is as follows but not working. Perhaps there may be an efficient way.
for k=1:175
[m]=data{1,k}
ave(:,k)=mean(Z1cell{1,m});
end
Thanks for the help in advance.

Accepted Answer

Kelly Kearney
Kelly Kearney on 24 Oct 2014
If I understand you correctly, I think the following will do what you want. To take the average, I horizontally concatenated the appropriate Z1cell values and specified a row-wise average. Also, you need to check for those empty index instances, since otherwise you'll get an improper assignment error there.
ave = nan(size(Z1cell{1},1), length(data));
for ii = 1:length(data)
if ~isempty(data{ii})
ave(:,ii) = mean(cat(2, Z1cell{data{ii}}), 2);
end
end
  4 Comments
Damith
Damith on 29 Oct 2014
Edited: Damith on 29 Oct 2014
Is there a way to modify the above code to manually provide a matrix of columns to take the average from rather than taking from the previous column?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 Oct 2014
Because m could have a variable number of rows, that means the mean will have that same variable number of rows and "ave" will have that same variable number of columns. That means ave should be a cell array. You can make ave a double, but it must be at least as wide as the largest length you ever expect m to be and you must pre-allocate it, and then you need to adjust the second index of ave so that it's not k but 1:length(m), and you will have some empty aves that just remain zero. Having it be a cell array is probably simpler.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!