how to creat this vector?

2 ビュー (過去 30 日間)
benghenia aek
benghenia aek 2019 年 1 月 30 日
編集済み: Stephen23 2019 年 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?

採用された回答

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

その他の回答 (1 件)

Stephen23
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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by