MATLAB: Matrix Manipulation from 4D to 3D

dimensionmatrix manipulation

Good morning Matlab Community, I hope all of you are doing well. I need some help with manipulating matrix dimensions. I have 4D matrix of the following dimension (5*5 by14680 by50), so in my own simple words, I have 50 sets of 14680 (5*5) matrices. What I would like to do is do the following operation for each matrix: Suppose for a given 5*5 matrix; get sum of 1st column, then the sum of the first row, then compute the difference (Sum of the 1st column minus sum of 1st row) [ Difference=(sum(:,1)-sum(1,:))] Next is basically manipulating the output: Final matrix should be of dimension (14680*50) (each row element contains a value for the difference calculate above) So this matrix is now 2D and I can just plot a curve for each row (change of the difference calculated from 1 to 50) I would greatly appreciate any guidance, Thank you so much, Amine

Best Answer

I think this does what you describe ...
% Some made-up data
M = rand(5,5,14680,50);
sumCol1 = squeeze(sum(M(:,1,:,:),1));
sumRow1 = squeeze(sum(M(1,:,:,:),2));
diffColRow = sumCol1 - sumRow1;
Also, you don't really need the intermediate variables:
diffColRow = squeeze(sum(M(:,1,:,:),1)) - squeeze(sum(M(1,:,:,:),2));