How Can I count the ascending runs in array ?

2 views (last 30 days)
Willim
Willim on 18 Feb 2019
Commented: Willim on 18 Feb 2019
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 Comments
KSSV
KSSV on 18 Feb 2019
diff shall be useful..read about it.
Willim
Willim on 18 Feb 2019
I used it
v=diff(x)
v(v>=0)=1
v(v<0)=0
but it give me wrong result for larger x

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 18 Feb 2019
Edited: Stephen23 on 18 Feb 2019
As KSSV wrote, you might find diff useful:
>> y = y([1,1:end])
y =
1 1 1 0 0 1 1 1

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!