Calculating moving average of a 3D matrix
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ana Marie
el 5 de Ag. de 2019
Comentada: Ana Marie
el 9 de Ag. de 2019
Hi All
I have a 3D matrix of size 412 x 314 x 200. I am squeezing out 2D matrices as follows:
Suppose A = 412 x 314 x 200
Matrix1 = squeeze(A(:,:,1));
Matrix2 = squeeze(A(:,:,2));
and so on. I can do this using a loop.
I want to calculate the moving average of these various Matrices (Matrix 1, Matrix 2 and so on). I am not able to understand how the 'movmean' function works. 'Smoothdata' does not work on my laptop. I am also unsure how to select the moving window, and which window would be appropriate for my data. Could someone please assist with this query?
Maybe there is a better way of calculating the moving average without having to squeeze matrices out? The ultimate result that I want should be a vector of size 200 x 1, where 200 is the third dimension of A and for every third dimension I want to calculate a final figure for 412 x 314 via moving average.
Thank you in advance for your help!
An
1 comentario
Walter Roberson
el 5 de Ag. de 2019
"I am also unsure how to select the moving window, and which window would be appropriate for my data."
What result are you trying to obtain that leads you to try moving average?
Suppose for a moment that you have 5 points ABCDE and that you wanted to do a moving average with window 3. Then one of the outputs would be (A+B+C)/3, and another would be (B+C+D)/3, and another would be (C+D+E)/3. That is 5-3+1 full windows. Now in your requirements you want the output of the moving average to be the same size as the original, 5 in this example. What would the calculation be for those two other outputs? (A+B)/2 and (D+E)/2? Or (0+A+B)/3 and (D+E+0)/3? Or (A+A+B)/3 and (D+E+E)/3?
Respuesta aceptada
Neuropragmatist
el 5 de Ag. de 2019
I don't quite understand what you want, you have a 3D matrix which is 412x314x200 and you want to get a 1x200 vector where each element i is the mean of a page in the original matrix? like this:
vector(i) = mean(matrix(:,:,i)) % not real syntax by the way
For that you can use mean like this:
vector = mean(mean(matrix,1),2)
Or maybe you want to get one 412x314 image which is the mean along the third dimension of the matrix, like this:
new_image = mean(matrix,3)
Or maybe you want to take each page of the 3D matrix and smooth it, then return it to the matrix and/or calculate some value from it?
If this is what you want to do though I suggest you look at imfilter or filter2, because movmean only works for vectors. You could use something like this to average filter each page of your 412x314x200 matrix:
input_matrix = rand(412,314,200);
average_window = 10; % this controls the size of the moving average filter - i.e. the filter will be [average_window x average_window]
average_filter = fspecial('average',[average_window average_window]);
B = imfilter(input_matrix,average_filter);
figure
subplot(1,2,1)
imagesc(input_matrix(:,:,1))
title('original (:,:,1)')
subplot(1,2,2)
imagesc(B(:,:,1))
title('average smoothed (:,:,1)')
Then you will have to decide what you want to do with that.
Like movmean and other smoothing functions imfilter needs to know the 'filter size', this is the size of the window over which it should work. For instance if the filter size is 11 then every pixel in the smoothed image is the average of the 11x11 square of pixels surrounding it in the original.
Hope this helps,
M.
3 comentarios
Neuropragmatist
el 6 de Ag. de 2019
True, I thought the OP might want some of the extra features afforded by imfilter like boundary correction, but as I'm not really sure what they want this might be wrong, convn should probably be faster.
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots 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!