Borrar filtros
Borrar filtros

how to creat this vector?

2 visualizaciones (últimos 30 días)
benghenia aek
benghenia aek el 30 de En. de 2019
Editada: Stephen23 el 30 de En. de 2019
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?

Respuesta aceptada

Rik
Rik el 30 de En. de 2019
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))

Más respuestas (1)

Stephen23
Stephen23 el 30 de En. de 2019
Editada: Stephen23 el 30 de En. de 2019
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

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by