Find places before or after zeros in vector

1 回表示 (過去 30 日間)
Christian S.
Christian S. 2021 年 8 月 26 日
コメント済み: Christian S. 2021 年 8 月 27 日
Hi MATLAB-Community,
I'm searching for an elegant way to mark the places before and after zeros in a Vector.
e.g.:
a = 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 0
before = 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0
after = 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0
Has anybody an Idea that can help me with that Problem?
Thanks in advance!
Cheers
Christian

採用された回答

Ive J
Ive J 2021 年 8 月 26 日
編集済み: Ive J 2021 年 8 月 26 日
a = [0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 0];
before = [0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0];
after = [0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0];
da = [diff(a), 0];
bcheck = circshift(da > 0, 1);
acheck = da < 0;
all(bcheck == before)
ans = logical
1
all(acheck == after)
ans = logical
1
This may not cover leading and trailing zeros, though before and after zeros may not make sense in that case.
  1 件のコメント
Christian S.
Christian S. 2021 年 8 月 27 日
Perfect, thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by