MATLAB: How to creat this vector

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?

Best Answer

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))