Summing groups of ones
Mostra commenti meno recenti
If I have a list of ones and zeros....the ones are clustered together in groups....how can I ask the program to sum the ones within their groups?
2 Commenti
Oleg Komarov
il 24 Apr 2012
Can you provide an example of input and output?
William
il 24 Apr 2012
This might be helpful: http://www.mathworks.com/matlabcentral/newsreader/view_thread/160813
Risposta accettata
Più risposte (2)
Rick Rosson
il 24 Apr 2012
y = cumsum(x);
d = x(2:end) - x(1:end-1);
k = ( d == -1 );
z = y(k);
Andrei Bobrov
il 24 Apr 2012
x1 = [~x(1) x(:)' ~x(end)]
groupones = diff([strfind(x1,[0 1]);strfind(x1,[1 0])]);
variant
x1 = x(:);
k = find([true;diff(x1)~=0]);
out = [x1(k) diff([k,[k(2:end);numel(x1)+1]],1,2)]
Categorie
Scopri di più su Surrogate Optimization in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!