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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by