How Can I count the ascending runs in array ?

1 回表示 (過去 30 日間)
Willim
Willim 2019 年 2 月 18 日
コメント済み: Willim 2019 年 2 月 18 日
if I have an array such as
x=[1 2 3 2 1 2 4 5]
I would generate a binary array for binary search
binaryarray= [1 1 1 0 0 1 1 1]
in binaryarry i would sign 1 along with ascending runs while 0 along with descending runs.
  2 件のコメント
KSSV
KSSV 2019 年 2 月 18 日
diff shall be useful..read about it.
Willim
Willim 2019 年 2 月 18 日
I used it
v=diff(x)
v(v>=0)=1
v(v<0)=0
but it give me wrong result for larger x

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

回答 (1 件)

Stephen23
Stephen23 2019 年 2 月 18 日
編集済み: Stephen23 2019 年 2 月 18 日
As KSSV wrote, you might find diff useful:
>> y = y([1,1:end])
y =
1 1 1 0 0 1 1 1

カテゴリ

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