MATLAB Answers

find where number changes in a matrix

10 views (last 30 days)
I have a matrix of +1 and -1 values. I want to find where the number flips from 1 to -1 or vice versa where the change is a change from column to column.
X=[...
1 1 1 1;
1 -1 1 1;
1 1 1 -1;
1 1 1 1]
the answer should be [2,2],[2,3]and [3,4]. I tried
find(diff(X)==2);
find(diff(X)==-2);
Any help is appreciated

  0 Comments

Sign in to comment.

Accepted Answer

Cedric Wannaz
Cedric Wannaz on 23 Mar 2013
>> [r,c] = find(diff(X,1,2)) ;
>> loc = [r, c+1]
loc =
2 2
2 3
3 4

  0 Comments

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 23 Mar 2013
One way:
[i j] = find(abs([zeros(4,1),diff(X,[],2)])==2)

  0 Comments

Sign in to comment.

Tags