Searching for a value in a column

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

 채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 11일

1 개 추천

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

James
James 2011년 4월 11일
Thanks Walter,
It's always a far simpler solution than you imagine!
Teja Muppirala
Teja Muppirala 2011년 4월 12일
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 2011년 4월 12일
Well done, Teja. I hadn't figured out a way to handle multiple zeroings; your code takes them in stride nicely.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by