Clear Filters
Clear Filters

Average matrices in a cell array within a structure Matlab

2 views (last 30 days)
Hi,
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?
  3 Comments
Maheen Siddiqui
Maheen Siddiqui on 20 Feb 2018
Yes, the data from the remainder of the larger arrays gets discarded.
Ok so the data in
AVG(1).averageNEST{1,1}(1:231,1,1)
gets averaged with
AVG(1).averageNEST{1,2}(1:231,1,1)
then
AVG(1).averageNEST{1,1}(1:231,2,1)
gets averaged with
AVG(1).averageNEST{1,2}(1:231,2,1)
the averaging is done over each of the columns in the second and 3rd dimensions.
Hope that makes sense.
Bob Thompson
Bob Thompson on 20 Feb 2018
Are the values being averaged really just the two consecutive arrays, or are you doing so for all six averageNEST arrays?
I'm sure Stephen could come up with something better, but I would just run a series of for loops.
for I = 1:size(AVG) % Loops through all AVG
for J = 1:size(averageNEST{1},3) % Loops through third dimension
for K = 1:size(averageNEST{1},2) % Loops through second dimension
average = mean(AVG(I).averageNEST{:}(1:min(size(averageNEST{:},1)),K,J);
end
end
end

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!