MATLAB Answers

Mar
0

Calculate the mean of two dimensions in a 3D array

Asked by Mar
on 4 May 2018
Latest activity Edited by KL
on 4 May 2018
I have a 3D array and I want to calculate the mean of the first two dimensions along the 3rd dimension but I am getting the following error
"Assignment has more non-singleton rhs dimensions than non-singleton subscripts".
My code looks as follows
MeanMatrix = zeros(1,1,30);
ImageROI = data(50:55,100:105,:);
for i=1:30
MeanMatrix(:,:,i) = mean(ImageROI(:,:,i));
end

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Guillaume
on 4 May 2018
Edited by Guillaume
on 4 May 2018
 Accepted Answer

mean on a 2D matrix (your imageROI(:, :, i)) will return a vector. You can't assign a vector to a scalar (which is what MeanMatrix(:, :, i) is since it has 1 row and 1 column).
In any case, the loop is completely unnecessary:
MeanMatrix = mean(mean(ImageROI, 1), 2);

  0 Comments

Sign in to comment.


Answer by KL
on 4 May 2018
Edited by KL
on 4 May 2018

If you want the mean of all rows in every page(3rd dimension)
mean(ImageROI,1)
for columns in every page
mean(ImageROI,2)
and mean of pages,
mean(ImageROI,3)
if you're looking to calculate the mean of all elements in a page, mean2 would be something
mean2(ImageROI(:,:,pageNo)) %with a loop
without a loop, probably with reshape,
mean(reshape(ImageROI,[],size(ImageROI,2),1))

  0 Comments

Sign in to comment.