How to check elements with value of 1’s in adjoining rows

조회 수: 1 (최근 30일)
Vishal Sharma
Vishal Sharma 2017년 2월 20일
답변: Guillaume 2017년 2월 20일
A = [1 1 0 0;
0 1 1 0;
0 1 1 0;
0 0 1 1;
0 0 1 1]
How to check the result of 1’s in adjoining rows are equal.
i.e. 2nd element in 1st and 2nd row have common 1
2nd and 3rd elements in 2nd and 3rd row have common 1’s
3rd elements in 3rd and 4th row have common 1
3rd and 4th elements in 4th and 5th row have common 1

답변 (1개)

Guillaume
Guillaume 2017년 2월 20일
~diff(A) & A(1:end-1, :)
will show you which element of each row is identical to the one in the next row. You can transform that into a cell array of indices with:
[r, c] = find(~diff(A) & A(1:end-1, :));
accumarray(r, c, [], @(x) {x})

카테고리

Help CenterFile Exchange에서 Cartesian Coordinate System Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by