Create a cumulative sum under certain conditions
1 回表示 (過去 30 日間)
古いコメントを表示
Hello together, I am pretty new to MATLAB since I has been forced to switch. Accordingly I have not many skills that would help me solve my problem wherefore I would appreciate any help!
My Problem: I have a matrix (actually it's an imported CSV-table) of following form:
- column1: contains numbers of days incrementing every 10th value (1,1,1,1,1,1,1,1,1,1,2,2,2,....,40)
- column2: contains names where each name only appears once a day (e.g. "Paul" would appear once on day 1,2,..,40).
- column3: cotains the output
My goal is to create a further column where I can create the cumulative sum of the column containing the output numbers.
For example:
On day 1 Paul has Output 5, on day 2 it is an output of 3 and on day 3 it is 4.
So in my newly created cumulative-sum-column (column4) the following should happen in every line:
- If column1 = 1, column2 = Paul, column3 = 5 then column4 = 5
- If column1 = 2, column2 = Paul, column3 = 3 then column4 = 8
- If column1 = 3, column2 = Paul, column3 = 4 then column4 = 12
Is there anybody able to help?
Many thanks in advance!
0 件のコメント
採用された回答
Bhaskar R
2019 年 11 月 17 日
編集済み: Bhaskar R
2019 年 11 月 17 日
Assuming your imported data from csv as variables Day, Name, Output all are column matrices with same length
I don't know variance of the conditions over here
Then take a dummy cumulative sum of column Output as column 4 value
csum = cumsum(Output);
Then take indeces from conditions as
column_4_value = csum(Day == 1 & strcmp(Name, 'Paul') & Output == 5);
Hope helps you !!
0 件のコメント
その他の回答 (1 件)
Akira Agata
2019 年 11 月 18 日
How about the following?
% Sample data
days = repelem((1:10)',10,1);
names = repmat(['A':'J']',10,1);
output = randi(100,100,1);
% Arrange them to create table variable
Data = table(days,names,output);
% Apply findgroups function
group = findgroups(Data.names);
% Apply cumsum function for each group
cSum = splitapply(@(x) {cumsum(x)},Data.output,group);
% Store the calculated result to 4th column
Data.result = nan(height(Data),1);
for kk = 1:max(group)
idx = group == kk;
Data.result(idx) = cSum{kk};
end
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!