Can the diff function go the opposite direction?

14 ビュー (過去 30 日間)
Teddy Fisher
Teddy Fisher 2019 年 12 月 18 日
コメント済み: Steven Lord 2019 年 12 月 19 日
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 件のコメント
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?
Steven Lord
Steven Lord 2019 年 12 月 19 日
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 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by