Trailing Sum Calculation using pre-defined kernel starting at incorrect cell

조회 수: 1 (최근 30일)
Table Z:
Goal: Calculating a 12-point trailing sum for any date point looking upward in the table, e.g. 20-May-2016 must display the sum of the shaded area (0.1768).
So far:
[rows, columns] = size(Z);
onesVector = ones(rows, 1);
kernel12t = [0;0;0;1;1;1;1;1;1;1;1;1;1;1;1]; % Kernel that computes
% the trailing 12 values
trailingSum12t = zeros(rows, columns);
for col = 4 : columns
thisColumn = Z{:, col}; % Extract all rows from this column.
cellSum12t = conv(onesVector, kernel12t, 'same');
valuesSum12t = conv(thisColumn, kernel12t, 'same');
trailingSum12t(:, col) = valuesSum12t;
end
The outcome of "trailingSum12t" is displayed below. However, the highlighted cell shouldn't be 0.1760 but 0.1768. Strangely I find the correct sum of the 12-point trailing kernel (0.1768) in a cell (row 8) where it shouldn't be.
Another issue is found in "cellSum12t" (see below): rows 551-554 should contain 12's!
The upper part of "cellSum12t" looks perfect though (see below):

채택된 답변

Michael Abboud
Michael Abboud 2017년 1월 13일
I believe the key issue you are facing is the term: 'same'. When you pass this argument to "conv", it will make the output array the same size as the first input argument (in this case, rows-by-1), however it discards values on either end. In contrast, you will likely want to keep the first 'rows' elements of the output. For more information on convolution, there is a nice explanation on wikipedia .
For example, it seems to me that your "cellSum12t" should look like the output of the following code:
fakeData = ones(32,1);
kernel = ones(12,1);
output = conv(fakeData,kernel);
output = output(1:length(fakeData))
The second possible issue is the extra zeros in your kernel. They are not fundamentally wrong, but from your description of what you'd like to achieve, they do not seem needed. If you want to sum 12 values, including the current one, you can just use:
"kernel = ones(12,1)"
If you want to sum the 12 values above the current (for example), you could use
"kernel = [0; ones(12,1)]"
  댓글 수: 2
John
John 2017년 1월 15일
편집: John 2017년 1월 15일
Many thanks, @Michael Abboud, this is very helpful! But for your last case I think it should be
kernel = [ones(12,1); 0]
, right? Like that, we would have a 12-point trailing sum, excluding the current one and including all 12 that come above.
Michael Abboud
Michael Abboud 2017년 1월 16일
I still believe that you would want it to say:
kernel = [0; ones(12,1)]
because of the 'flip and slide' process of convolution. If you look here on Wikipedia there is a nice visual. In particular, because of the 'flip' in step 2, I think you would want the zero on top here.
1. Get your data and your filter
2. Flip one of them (often the filter)
3. As you slide the filter across the data, multiply and sum the 'overlapping' portions
That being said... you're the one with the data, so you can test to see which works. It may be that your setup is slightly different than I understood :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by