Cumulative Summation down a matrix in loop

1 回表示 (過去 30 日間)
IDN
IDN 2022 年 2 月 9 日
コメント済み: Adam Danz 2022 年 2 月 10 日
Hello!
I have 2 matrix, I would like to sum Matrix B values cummulative given condition. The condition is that it starts to sum once Matrix A = -1 and stops when Matrix A = 1 and it goes on and on all the way down to the end of the data set. Thanks for the help!
Matrix A Matrix B CumSum
0
0
0
-1 0
-1 0.02
-1 -12.09
-1 6.61
1 1.1 -4.36 CumSum
0 0
-1 0
-1 -6.8
-1 -26.87
-1 2.67
-1 -9.99
-1 9.28
-1 -3.17
1 8.6 7.39 CumSum
0
0
0
  2 件のコメント
Turlough Hughes
Turlough Hughes 2022 年 2 月 9 日
Do you mean -4.36 and -26.28?
IDN
IDN 2022 年 2 月 9 日
編集済み: IDN 2022 年 2 月 9 日
those are the totals when cumum runs by summing values in Matrix B. So -4.36 and -26.28 is the expected answer when all set an done. Thanks for helping!

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2022 年 2 月 9 日
編集済み: Adam Danz 2022 年 2 月 9 日
If and only if the groups marked by A=-1 to A=1 are not interruped by any other values in A and a 1 does not appear before the first -1, the solution is,
T = readtable('Sum Sample.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 20×3 table
MatrixA MatrixB CumSum _______ _______ ______ 0 NaN NaN 0 NaN NaN 0 NaN NaN -1 0 NaN -1 0.02 NaN -1 -12.09 NaN -1 6.61 NaN 1 1.1 -4.36 0 0 NaN -1 0 NaN -1 -6.8 NaN -1 -26.87 NaN -1 2.67 NaN -1 -9.99 NaN -1 9.28 NaN -1 -3.17 NaN
startIdx = find([false;diff(T.MatrixA==-1)==1]);
stopIdx = find([false;diff(T.MatrixA==1)==1]);
groupSums = arrayfun(@(start,stop)sum(T.MatrixB(start:stop)),startIdx,stopIdx)
groupSums = 2×1
-4.3600 -26.2800
Or perhaps you want,
T.CumSum(stopIdx) = groupSums
T = 20×3 table
MatrixA MatrixB CumSum _______ _______ ______ 0 NaN NaN 0 NaN NaN 0 NaN NaN -1 0 NaN -1 0.02 NaN -1 -12.09 NaN -1 6.61 NaN 1 1.1 -4.36 0 0 NaN -1 0 NaN -1 -6.8 NaN -1 -26.87 NaN -1 2.67 NaN -1 -9.99 NaN -1 9.28 NaN -1 -3.17 NaN
  4 件のコメント
IDN
IDN 2022 年 2 月 9 日
Got it!
Adam Danz
Adam Danz 2022 年 2 月 10 日
Just saw your message now. Glad you worked it out. That line merely places data within the table T so nothing should be coming out horizontally.

サインインしてコメントする。

その他の回答 (1 件)

Highphi
Highphi 2022 年 2 月 9 日
matrixA = [0 0 0 -1 -1 -1 -1 1 0 -1 -1 -1 -1 -1 -1 -1 1 0 0 0]'; % for ref
matrixB = [0 0 0 0 0.02 -12.09 6.61 1.1 0 0 -6.8 -26.87 2.67 -9-99 9.28 -3.17 8.6 0 0 0]'; % for ref
state = 0;
matrixSums = zeros(size(matrixA,1), 1);
for i = 1:size(matrixA, 1)
temp = matrixA(i);
if (temp == -1) && (state == 0)
state = 1;
cumSum = matrixB(i);
elseif (state == 1) && (temp ~= 1)
cumSum = cumSum + matrixB(i);
elseif (state == 1) && (temp == 1)
cumSum = cumSum + matrixB(i);
matrixSums(i) = cumSum;
state = 0;
end
end
outMat = [matrixA, matrixB, matrixSums]
outMat = 20×3
0 0 0 0 0 0 0 0 0 -1.0000 0 0 -1.0000 0.0200 0 -1.0000 -12.0900 0 -1.0000 6.6100 0 1.0000 1.1000 -4.3600 0 0 0 -1.0000 0 0
  2 件のコメント
IDN
IDN 2022 年 2 月 9 日
Thanks, this looks good as well...i just dont have the toolbox required for the "state" functionality
Highphi
Highphi 2022 年 2 月 9 日
you shouldn't need a toolbox, it's just a variable to indicate your status

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by