how to creat this vector?

2 ビュー (過去 30 日間)
benghenia aek
benghenia aek 2019 年 1 月 29 日
編集済み: Stephen23 2019 年 1 月 29 日
hello everyone
I have vector
X=[1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0]
i need this transformation
if Nbr of 1 >3 the vector become:
Y=[0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0]

採用された回答

Stephen23
Stephen23 2019 年 1 月 29 日
編集済み: Stephen23 2019 年 1 月 29 日
An easy solution using a loop:
>> X = [1,0,0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,1,0]
X =
1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0
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
X(B(k):E(k)) = 0;
end
end
>> X
X =
0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0

その他の回答 (2 件)

Jan
Jan 2019 年 1 月 29 日
The question is not clear, but I assume you mean: set values to 0, if less than 3 neighboring elements are 1.
X = [1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0];
[B, N] = RunLength(X);
B(N < 3) = 0;
Y = RunLength(B, N);
If you do not have a compiler installed, use RunLength_M from the same submission.

Torsten
Torsten 2019 年 1 月 29 日
X=[1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0];
if sum(X)>3
X=[0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0];
end

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by