Maheen Siddiqui
Maheen Siddiqui

Reputation: 539

Average matrices in a cell array within a structure Matlab

In Matlab I have a structure AVG of size 1 x 6 which has one field averageNEST that is a cell array also of size 1 x 6.

Each averageNEST contains matrices of varying sizes (in one dimension), so for example

AVG(1).averageNEST{1,1} is of size 281 x 3 x 19 and

AVG(1).averageNEST{1,2} is of size 231 x 3 x 19

The 2nd and 3rd dimensions of the matrices are always 3 and 19, it is only the first dimension that can change.

I want to average over all the matrices contained within AVG(1).averageNEST and obtain one matrix of size X x 3 x 19 where X is the size of the smallest matrix in AVG(1).averageNEST.

Then I want to do this for all 6 averageNEST in AVG - so have a separate averaged matrix for AVG(1), AVG(2) ... AVG(6).

I have tried multiple things including trying to concatenate matrices using the following code:

 for i=1:6
    min_epoch = epoch + 1;
         for ii=1:19
            averageNEST(:,:,ii) = [AVG(i).averageNEST(1:min_epoch,:,ii)];
         end
 end

and then average this but it doesn't work and now I'm really confused about what I'm doing!

Can anyone help?

Upvotes: 0

Views: 120

Answers (1)

Jorge Pérez
Jorge Pérez

Reputation: 96

I am not sure if I understand what you want to do. If you want to keep only the elements up to the size of the the smallest matrix and then average those matrices you can do the following:

averageNEST = cell(size(AVG)); 
for iAVG = 1:numel(AVG)
    nests = AVG(iAVG).averageNEST;
    minsize = min(cellfun(@(x) size(x,1), nests));    
    reducednests = cellfun(@(y) y(1:minsize, :, :), nests, 'UniformOutput', false);
    averageNEST{iAVG} = sum(cat(4, reducednests{:}), 4) / numel(nests);
end

Upvotes: 0

Related Questions