I'm pretty new to Matlab and not able to solve the following problem:
I've got three column vectors, "Day", "Name", "Values" each 300x1 and I need to sum up "Values" (cumulative) in a certain range which depend not only on the vector "Name" but also the vector "Day".
More precisely, I want to sum up the values in vector "Values" for each category (vector "Name") but only for a certain range (vector "Day"), let's say I want to sum up all the "Values" starting on "Day" 2, the "Result"-vector should look like (shortened example):
| Day | Name | Values | Result |
|-----|------|--------|------------|
| 1 | A | 2 | 0 |
| 1 | C | 9 | 0 |
| 1 | B | 7 | 0 |
| 2 | D | 1 | 1 |
| 2 | B | 1 | 1 |
| 2 | A | 3 | 3 |
| 2 | D | 4 | 5 |
| 3 | D | 9 | 14 |
| 3 | B | 1 | 2 |
| 3 | C | 3 | 3 |
| 3 | A | 1 | 4 |
| 4 | D | 3 | 17 |
Later on I would like to change the range, e.g. starting to sum up after "Day" 2, "Day" 3 and so on which would need to be adjustet for the starting "Day".
For my sum range starting on "Day" 2 I've created the following code but it does not work at all:
Result = zeros(size(Values));
a=unique(Name);
for k=1:length(a)
for i = 1:length(Name)
if Day(i) > 1
Result(ismember(Name,a(k)))=cumsum(Values(ismember(Name,a(k))));
end
end
end
Is there anybody able to help? Many thanks in advance!
Aucun commentaire:
Enregistrer un commentaire