Mean of columns within matrix blocks of different dimensions
3 views (last 30 days)
Show older comments
I would like to create a matrix M where each column equals to the mean of the matrix blocks of the matrix A.
Case 1: The code below works well when I have subarrays of always 3 columns
B = reshape(A,2151,3,28);
M = squeeze(mean(B,2));
Case 2: But, now I need to group the columns of the matrix A either by 3 or 4.
B = mat2cell(A,2151,[3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 3 3 3]);
I created subarrays with mat2cell, but then I don't know how to average each blocks. The squeeze function doesn't work anymore.
Is it possible to produce a similar output than in Case 1 when I have subarrays of different dimensions ?
Thanks in advance for your help
0 Comments
Accepted Answer
Sriram Tadavarty
on 4 Apr 2020
Hi Fpet,
You can try the following:
B = mat2cell(A,2151,[3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 3 3 3]);
bAvgCell = arrayfun(@(x) mean(B{x},2),1:length(B),'UniformOutput',false);
M = cell2mat(bAvgCell);
Hope this helps.
Regards,
Sriram
More Answers (0)
See Also
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!