2015-05-09 10 views
8

Załóżmy, że posiada dwa wejścia i wektory xreset tej samej wielkościzbiorczej sumowania w odstępach - MATLAB

x = [1 2 3 4 5 6] 
reset = [0 0 0 1 0 0] 

a wyjściem y czyli łączna suma elementów x. Ilekroć wartość resetuje odpowiada 1, łączna suma dla elementów zresetować i zacząć wszystko od nowa, tak jak poniżej

y = [1 3 6 4 9 15] 

Jak zaimplementować to w Matlab?

Odpowiedz

7

Jedno podejście z diff i cumsum -

%// Setup few arrays: 
cx = cumsum(x)   %// Continuous Cumsumed version 
reset_mask = reset==1 %// We want to create a logical array version of 
         %// reset for use as logical indexing next up 

%// Setup ID array of same size as input array and with differences between 
%// cumsumed values of each group placed at places where reset==1, 0s elsewhere 
%// The groups are the islands of 0s and bordered at 1s in reset array. 
id = zeros(size(reset)) 
diff_values = x(reset_mask) - cx(reset_mask) 
id(reset_mask) = diff([0 diff_values]) 

%// "Under-compensate" the continuous cumsumed version cx with the 
%// "grouped diffed cumsum version" to get the desired output 
y = cx + cumsum(id) 
+0

Hej, to działa świetnie, ale można byłoby w stanie wyjaśnić tej części kodu. id (reset == 1) = diff ([0 diff1 (reset == 1)]) – Alex

+0

@Alex Pewnie, zbliża się. – Divakar

+0

Dziękuję bardzo. Od jakiegoś czasu drapie mi to w głowę. – Alex

4

Oto sposób:

result = accumarray(1+cumsum(reset(:)), x(:), [], @(t) {cumsum(t).'}); 
result = [result{:}]; 

To działa, ponieważ jeśli pierwsze wejście do accumarray jest posortowana, kolejność w obrębie każdej grupy do drugiego wejścia jest zachowana (więcej o tym here).

Powiązane problemy