Calculate the mean of two dimensions in a 3D array

41 Ansichten (letzte 30 Tage)
Mar am 4 Mai 2018
Kommentiert: Rohit shaw am 10 Okt. 2021
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));

Akzeptierte Antwort

Guillaume am 4 Mai 2018
Bearbeitet: Guillaume am 4 Mai 2018
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);
  1 Kommentar
Rohit shaw
Rohit shaw am 10 Okt. 2021
Hello, could you kindly tell me what would the command be if I have to use this command across 86 timesteps. I have a 3d array of 720x360x86. These are temperature values for the world.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KL am 4 Mai 2018
Bearbeitet: KL am 4 Mai 2018
If you want the mean of all rows in every page(3rd dimension)
for columns in every page
and mean of pages,
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,


Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by