필터 지우기
필터 지우기

Calculating moving average of a 3D matrix

조회 수: 23 (최근 30일)
Ana Marie
Ana Marie 2019년 8월 5일
댓글: Ana Marie 2019년 8월 9일
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
Walter Roberson
Walter Roberson 2019년 8월 5일
"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?

댓글을 달려면 로그인하십시오.

채택된 답변

Neuropragmatist
Neuropragmatist 2019년 8월 5일
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
Neuropragmatist
Neuropragmatist 2019년 8월 6일
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.
Ana Marie
Ana Marie 2019년 8월 9일
Thank you Metioche and Walter. The process is now clear to me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by