Matlab's Movsum() doesn't start calc at defined starting point X in frame [X X+t]
조회 수: 1 (최근 30일)
이전 댓글 표시
Suppose the following:
A = [1;2;3;4;5;6;7];
A1 = table(A);
The following sums current and 2 following cells beneath:
A2 = movsum(A1{:, 1:end}, [0 2], 'Endpoints', 'fill');`
What I want is the sum of what above movsum() does, but without considering the current cell (0 in [0 2]). The very first value of this calculation should be 5 (2+3) (and appear in the first row of table A3), the second 7 (3+4) etc. How do I have to adapt my following code for it to do the intended calculations? How about if I wanna take this rationale one step further, i.e. exclude current + following cell from my movsum() calculation?
A3 = movsum(A1{:, 1:end}, [1 2], 'Endpoints', 'fill');
... because A3 returns:
댓글 수: 0
답변 (1개)
Steven Lord
2017년 1월 27일
For the first, eliminating the current cell:
A = 1:7;
B1 = movsum(A, [0 2]);
B2 = A; % Technically movsum(A, [0 0]) but that's just A itself
C = B1-B2
For the second, eliminating the current and next cell:
D = 1:7;
E1 = movsum(D, [1 2]);
E2 = movsum(D, [0 1]);
F = E1-E2
If you want more complicated patterns, treat the windows as sets and use the inclusion-exclusion principle to determine which movsum calls you need to add or subtract. For instance, to get a window like:
pattern = [1 0 1 0 1]
where 1 means it's counted and 0 means it's not, you can write this as:
[1 1 1 0 0] ... % movsum(A, [2 0]), two previous points plus current point
- [0 1 1 1 0] ... % movsum(A, [1 1]), one previous point, current point, one next point
+ [0 0 1 1 1] % movsum(A, [0 2]), current point plus two next points
This is
A = (1:10).^2;
B = movsum(A, [2 0]) - movsum(A, [1 1]) + movsum(A, [0 2])
As a spot check, according to the pattern B(5) should be A(3)+A(5)+A(7) and it is.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!