Can the diff function go the opposite direction?

Hello,
As I understand diff it goes either left to right along rows or up to down a column. Is there a way to make diff do the same thing it currently does but go right to left or down to up?
Thanks,
Teddy

 채택된 답변

Jeremy
Jeremy 2019년 12월 18일
sure, flip your matrix with
fliplr
or
flipud
and then run
diff

댓글 수: 3

Hi, thanks for your answer, that is good to know.
I should clarify my case a bit, because I tried that and it does work.
However, for my case I am trynig to use diff to find the indices of stand alone values in a logical array. To do this, I am trying to find where the diff of a cell is either 1 or -1 in both directions, i.e. a single 1 in a series of 0s or vice versa. So using these flip commands changes the indices.
Is there another way to do this?
Jeremy
Jeremy 2019년 12월 19일
Wouldn't you fliplr the original array, call diff on it, and then flip the resulting array again to get what you want?
So you want to detect when the abs-olute diff-erence between adjacent elements is exactly 1?
If you know that your data only contains 0 and 1 (or false and true) it's even easier than using abs and diff.
x = randi([0 1], 1, 10)
d = [x(1:end-1) ~= x(2:end), 0]
[x; d]
Each element of d is true when the corresponding element of x differs from the next element in x. I set the last element of d to false because it doesn't make sense to ask if the last element is different from the nonexistent next-after-the-last element.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2019년 12월 18일

댓글:

2019년 12월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by