I am wondering if there is a (simply) way when using movmean for controlling row overflow and for specifying a rectangular window instead of a square window. The idea would be to keep averaging the patch corresponding to each number in red until the end of row is found, and simply jump to the next row. Thanks!

 채택된 답변

Matt J
Matt J 2022년 11월 13일
편집: Matt J 2022년 11월 13일
Here's one way. I assumed here you want the same wrap-around to occur in the lower-right corner of the matrix as well.
A=reshape(1:24,[],4)'
A = 4×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
B=[A,circshift(A(:,1:2),-1) ]
B = 4×8
1 2 3 4 5 6 7 8 7 8 9 10 11 12 13 14 13 14 15 16 17 18 19 20 19 20 21 22 23 24 1 2
slidingMeans=conv2(B,ones(3)/9,'valid')
slidingMeans = 2×6
8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 15.3333 13.6667

댓글 수: 5

Albert Zurita
Albert Zurita 2022년 11월 14일
Thanks!! No, I don't want the wrap at the lower-right corner. Should I replace the numbers by NaN? In addition, for a rectangular window, the circshift I assume should change to accommodate the rows/columns definition of the window?
Should I replace the numbers by NaN?
That's not for me to decide. However, you could do that and also omit nans from the mean calculation in this alternative forumulation:
A=reshape(1:24,[],4)'
A = 4×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
B=[A,circshift(A(:,1:2),-1) ];
B(end,end-1:end)=nan
B = 4×8
1 2 3 4 5 6 7 8 7 8 9 10 11 12 13 14 13 14 15 16 17 18 19 20 19 20 21 22 23 24 NaN NaN
t=movmean(B,3,2,'omitnan','End','discard');
slidingMeans=movmean(t,3,1,'End','discard')
slidingMeans = 2×6
8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 17.8333 18.6667
In addition, for a rectangular window, the circshift I assume should change to accommodate the rows/columns definition of the window?
Yes indeed.
Thanks for the tips. I have modified the code to adapt to a rectangular window as follows:
win = [3 5]; % rows, cols
A=reshape(1:24,[],4)'
B=[A,circshift(A(:,1:win(2)),-1)]
B(end,end-1:end)=nan
t=movmean(B,win(2),2,'omitnan','End','discard');
slidingMeans=movmean(t,win(1),1,'End','discard')
However, I am not fully convinced of the -1 in the circhift (if that is dependent on the window size), and with respect to the NaNs, I just want to stop averaging at the lower right border when I cannot complete the window box. To be illustrative I am exactly looking after obtaining the averages of the following boxes, for a window of [3 rows, 5 columns] and as a result obtain 8 numbers resulting from the 8 sequences below. Thanks!
win = [3 5]; % rows, cols
A=reshape(1:24,[],4)'
A = 4×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
B=[A,circshift(A(:,1:win(2)-1),-1)];
B(end,end-win(2)+2:end)=nan
B = 4×10
1 2 3 4 5 6 7 8 9 10 7 8 9 10 11 12 13 14 15 16 13 14 15 16 17 18 19 20 21 22 19 20 21 22 23 24 NaN NaN NaN NaN
slidingMeans=conv2(B,ones(win)/prod(win),'valid' )
slidingMeans = 2×6
9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 NaN NaN NaN NaN
Albert Zurita
Albert Zurita 2022년 11월 15일
This is excellent, and good methodology I learnt, thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 11월 13일

댓글:

2022년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by