how to creat this vector?

2 Ansichten (letzte 30 Tage)
benghenia aek
benghenia aek am 30 Jan. 2019
Bearbeitet: Stephen23 am 30 Jan. 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?

Akzeptierte Antwort

Rik
Rik am 30 Jan. 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))

Weitere Antworten (1)

Stephen23
Stephen23 am 30 Jan. 2019
Bearbeitet: Stephen23 am 30 Jan. 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

Kategorien

Mehr zu Software Development Tools finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by