sorting and removing binary data points
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!