How to average first two dimension of 3D array/matrix?
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
UTKARSH VERMA
el 1 de Sept. de 2021
Comentada: UTKARSH VERMA
el 1 de Sept. de 2021
Hi all,
I am calculating the average of first two dimension of a 3D matrix but it is showing following error:
Error using sum
Dimension argument must be a positive integer scalar within indexing range.
Error in mean (line 116)
y = sum(x, dim, flag) ./ size(x,dim);
Also, I have used a simplest example given in topic 'Mean of Array Page' in https://in.mathworks.com/help/matlab/ref/mean.html but I am getting same error.
I am using MATLAB 2018a
Please help!
Thanks in advance
2 comentarios
Respuesta aceptada
Más respuestas (2)
Wan Ji
el 1 de Sept. de 2021
Editada: Wan Ji
el 1 de Sept. de 2021
Just use mean function to average first two dimension of 3D matrix
a = rand(2,3,4); % I just use rand function so the result is randomly displayed
b = mean(a,1:2) % 1:2 means the first two dimension
The result is
b = mean(a,1:2)
b(:,:,1) =
0.5258
b(:,:,2) =
0.2759
b(:,:,3) =
0.6214
b(:,:,4) =
0.4449
If you want to get array of b
Then
b = squeeze(b) % or b = b(:);
So
b =
0.5258
0.2759
0.6214
0.4449
Steven Lord
el 1 de Sept. de 2021
The ability to specify a vector of dimensions over which to sum an array was introduced in release R2018b as stated in the Release Notes.
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!