sorting and removing binary data points

2 vues (au cours des 30 derniers jours)
Randall
Randall le 26 Août 2015
Commenté : Star Strider le 26 Août 2015
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!

Réponses (1)

Star Strider
Star Strider le 26 Août 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 commentaires
Randall
Randall le 26 Août 2015
This was really helpful, thanks!
Star Strider
Star Strider le 26 Août 2015
My pleasure!
If my Answer solved your problem, please Accept it.

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by