vesp
vesp

Reputation: 13

Averaging Across All Matrices Stored in Structure

I am currently using the following code to get an average across 8766 matrices stored in a structure, matData, but when I look inside Mcell (1x8766 cell) all of the values stored in each cell are duplicates of that in cell 1x1. I would like to know what I am doing wrong, since I will then take the nanmean of all the matrices in this structure.

Mcell = arrayfun(@(x) matData(sprintf('(%d)',x)).shape, 1:8766, 'uni', 0);
M = nanmean( reshape(cell2mat(Mcell), 192, 144, []), 3 );

Extra notes: matData is 1x8766 struct with 1 field files in matdata are called matData(i).shape where i=1:8766 and are 192x144 double.

Thank you for all of your input and help.

Upvotes: 1

Views: 41

Answers (1)

nirvana-msu
nirvana-msu

Reputation: 4077

You just need a combination of struct2cell, cell2mat and nanmean:

matData = cell2struct(num2cell(randn(192,144,8766),[1,2]), 'shape', 1); % Sample input

result = nanmean(cell2mat(struct2cell(matData)),3);

Upvotes: 1

Related Questions