Searching for a value in a column

7 views (last 30 days)
Hi,
I have a column with incrementing data which occasionally resets to zero. How do I search the column and check that each value isn't smaller than the previous entry? From that point I can simply add the previous value to all future value to remove the zeroing error, but I can't figure out how to scan down the column.
Cheers

Accepted Answer

Walter Roberson
Walter Roberson on 11 Apr 2011
find(diff(YourColumn) < 0)
There might be a way to do your entire operation in one or two commands; it doesn't immediately come to mind, though.
  3 Comments
Teja Muppirala
Teja Muppirala on 12 Apr 2011
Here's one way to do it in one line:
x = [10 14 17 2 5 6 1 2 3]
x(1) + [0 cumsum( max(diff(x),0) + (diff(x) < 0).*x(2:end))]
Walter Roberson
Walter Roberson on 12 Apr 2011
Well done, Teja. I hadn't figured out a way to handle multiple zeroings; your code takes them in stride nicely.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!