how to creat this vector?
1 次查看(过去 30 天)
显示 更早的评论
hello everyone;
i have vector
X=[1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0]
c= size number of 1 from the vector X
c=[2 3 5 1 4]
h=mean(c)
h=3;
if c>h
c=c;
elseif c<=h
c=0;
end
Y=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0]
how to creat vector Y?
0 个评论
采纳的回答
Rik
2019-1-30
I'm going to guess you mean this instead, and wanted to create Y from X accordingly.
X=[1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0];
c=[2 3 5 1 4];
h=mean(c);
for k=1:numel(c)
if c(k)>h
c(k)=c(k);
elseif c(k)<=h
c(k)=0;
end
end
This can be solved with the code below.
X=[1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0];
encoded=bwlabel(X);
c=histc(encoded,1:max(encoded));
ind=find(c<=mean(c));
Y=X;
Y(ismember(encoded,ind))=0;
Y_check=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0];
isequal(Y,Y_check)
%isequal might return false for floats, if so, check if this is small:
%max(abs(Y-Y_check))
0 个评论
更多回答(1 个)
Stephen23
2019-1-30
编辑:Stephen23
2019-1-30
Exactly like in my answer to your earlier question:
X=[1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0]
Y = X;
D = diff([0,X,0]);
B = find(D>0);
E = find(D<0)-1;
N = 3;
for k = 1:numel(B)
if (E(k)-B(k))<N
Y(B(k):E(k)) = 0;
end
end
Y
Giving:
X =
1 1 0 1 1 1 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0
Y =
0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!