collapse across dimensions of a matrix
Show older comments
I have a multidimensional matrix 19 x 64 x 5 x 21 which corresponds to 19 participants x 64 channels x 5 timepoints x 21 data values.
I want to collapse the last two dimensions of the matrix so that I end up with the mean of the (5 x 21) data values, per participant, per channel (i.e. a 19 x 64 (x1) matrix)
I've tried using mean, tried using reshape etc but I am really struggling with the scale of the data and picturing how the the four dimensions fit together. As such I can't confidently manipulate the data across the four dimensions so any guidance would be appreciated.
Accepted Answer
More Answers (4)
Mark Sherstan
on 21 Dec 2018
Give this a try:
% Generate random matrix
A = randi(10,[19 64]);
B = cat(3,A,A,A,A);
C = cat(4,B,B);
% Get indices and set counter
[idx1 idx2 idx3 idx4] = size(C);
counter = 1;
% Build cell array of all the matrices
for ii = 1:idx4
for jj = 1:idx3
out{counter} = C(:,:,idx3,idx4);
counter = counter + 1;
end
end
% Calculate the mean
dim = ndims(out{1});
M = cat(dim+1,out{:});
meanMatrix = mean(M,dim+1);
1 Comment
madhan ravi
on 21 Dec 2018
doc permute
Matt J
on 21 Dec 2018
mean(yourMatrix(:,:,:),3,'omitnan')
Matt J
on 21 Dec 2018
sepblockfun(yourArray, [1,1,inf,inf], 'mean')
Categories
Find more on Logical 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!