MATLAB Answers

How to find the index in array?

2 views (last 30 days)
Meshooo
Meshooo on 3 Aug 2016
Commented: Meshooo on 4 Aug 2016
Dear all,
I have the following array A = [0;0;0;0;0;0;1;1;;0;0;0;0];
I want to find the right and left index of the 1's? In this case
Left_A = 7
Right_A = 8
In another situations, A = [0;0;0;0;0;0;1;0;0;1;0;0;0];
Left_A = 7
Right_A = 10
Any idea how to do that? Thank you very much.
Meshoo

  0 Comments

Sign in to comment.

Accepted Answer

Adam
Adam on 3 Aug 2016
Left_A = find( A == 1, 1 );
Right_A = find( A == 1, 1, 'last' );

  1 Comment

Meshooo
Meshooo on 4 Aug 2016
That works very well. Thank you very much.

Sign in to comment.

More Answers (0)

Sign in to answer this question.