sorting and removing binary data points

2 ビュー (過去 30 日間)
Randall
Randall 2015 年 8 月 26 日
コメント済み: Star Strider 2015 年 8 月 26 日
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!

回答 (1 件)

Star Strider
Star Strider 2015 年 8 月 26 日
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 件のコメント
Randall
Randall 2015 年 8 月 26 日
This was really helpful, thanks!
Star Strider
Star Strider 2015 年 8 月 26 日
My pleasure!
If my Answer solved your problem, please Accept it.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by