sorting and removing binary data points
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a vector composed of binary data and I want to remove all elements of the data (or convert them to zero) where the row of 1's is less than 15 in a row. What would be the easiest way to do this?
Thanks!
0 Commenti
Risposte (1)
Star Strider
il 26 Ago 2015
This is probably more efficient than it looks. It has the virtue of working (at least with the various test vectors I checked it on):
V = randi([0 4], 1, 1000); % Create Data
V(501:520) = ones(1,20); % Create Data
V(701:720) = ones(1,20); % Create Data
dV = diff([0 V]); % Differences Are Beginning-End Indicators
rs = find(dV > 0); % ‘rs’: Run Start
re = find(dV < 0); % ‘re’: Run End
if length(re) < length(rs)
re = [re length(V)]; % If ‘re’ Ends At The End Of ‘V’, Add That
end
rd = re - rs; % ‘rd’: Run Diffrerences = Length Of Each Run
rrs = rs(rd > 15); % ‘rrs’: Result Run Start = Select Runs > 15
rre = re(rd > 15); % ‘rre’: Result Run End = Select Runs > 15
Vout = zeros(size(V));
for k1 = 1:length(rrs)
Vout(rrs(k1):rre(k1)) = V(rrs(k1):rre(k1));
end
figure(1)
plot(V) % Plot Original Vector
hold on
plot(Vout, 'LineWidth',2) % Plot Output Vector
hold off
grid
2 Commenti
Vedere anche
Categorie
Scopri di più su Annotations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!