# MATLAB: An accumulating matrix to be reset when the limit value is reached, the value being reset must be moved to another matrix

treshold

Hello
``B = [1, 4, 3, 1, 3, 2, 1, 0, 0, 1, 5, 6, 9, 1, 3]A = cumsum (B);A = [1, 5, 8, 9, 12, 14, 15, 15, 15, 16, 21, 27, 36, 37, 40]``
``A = [1, 0, 3, 4, 0, 2, 3, 3, 3, 4, 0, 0, 0, 1, 3] % Every time it exceds 5, the value has to be moved to C, and reset to 0C = [0, 5, 0, 0, 7, 0, 0, 0, 0, 0, 9, 6, 9, 0, 0]``
``B = [1, 4, 3, 1, 3, 2, 1, 0, 0, 1, 5, 6, 9, 1, 3];A = cumsum (B);C = zeros(size(A));idx=find(A>=5);while ~isempty(idx)    idx=idx(1);    C(idx)=A(idx);    A(idx:end)=A(idx:end)-A(idx);        idx=find(A>=5);endFormatSpec=[repmat('%d, ',1,numel(A)) '\n'];FormatSpec(end-3)='';clcfprintf(FormatSpec,A)fprintf(FormatSpec,C)``
``B = [1, 4, 3, 1, 3, 2, 1, 0, 0, 1, 5, 6, 9, 1, 3];A = cumsum (B);C = zeros(size(A));idx=find(A>=5);while ~isempty(idx)    idx=idx(1);    C(idx)=A(idx);    A=A-A(idx);        idx=find(A>=5);endA=cumsum(B)-cumsum(C);%restore real A``