remove single outliers from vector

1 次查看(过去 30 天)
the device creates a vector for me where only double values are true, and all the rest I need to zero
close all
clear all
clc
x=[ 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 ];
y1need=[ 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 ];
%
y=find(x>0);
%
for i = 1:length(x)-1
if y(i)+1<y(i+1)
y1(i)=0
else
y1(i)=y(i)
end
end
% p.c. code works but gives error like this, how to fix it?
Index exceeds the number of array elements (13).
Error in Test (line 9)
if y(i)+1<y(i+1)
% p.c. 2 is there any special functions in matlab for this?

采纳的回答

KSSV
KSSV 2021-4-1
x=[ 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 ];
y = [ 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 ];
ii = zeros(size(x));
jj = x > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
iwant = x ;
for i = 1:length(unique(idx))
if length(idx(idx==i)) == 1
iwant(idx==i) = 0 ;
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by