how to count identical elements in a non-symmetrical matrix?
조회 수: 1 (최근 30일)
이전 댓글 표시
Suppose we have a binary, non-symmetrical, 4x4 matrix, like this one:
exercise = [ 0 1 1 0 ; 1 0 0 1 ; 0 0 0 1 ; 1 1 0 0]
What I would like to do is, I'd like to count the identical non-zero elements in it, meaning, after comparing the 1st row and the 1st column of this matrix, I'd like to see the total of overlapping elements. Let me show you an example.
1st row of the matrix: 0 1 1 0
1st column of the matrix: 0 1 0 1
1st element of the 1st row and 1st column: both zeros, but we are interested in the non-zero elements so we don't count this one. 2nd element of the 1st row and 1st column: both are ones. We are looking for such cases so we register this one. 3rd element of the 1st row is 1, 3rd element of 1st column is zero. They are not equal so we move on. Next: 4th element of the 1st row is zero, 4th element of the 1st column is 1. These are not equal either. To sum up, we can clearly see that the 2nd elements equal, so overall we have one non-zero element in common. Let's see the next example:
2nd row of the matrix: 1 0 0 1
2nd column of the matrix: 1 0 0 1
Here we have two equalling non-zero elements. Let's see the next:
3rd row: 0 0 0 1
3rd column: 1 0 0 0
Here we don't have any overlapping non-zero elements at all. Last but not least,
4th row: 1 1 0 0
4th column: 0 1 1 0
One non-zero element in common here. What would really be awesome if there was a way to list the count of matches, maybe like this:
(1) 1
(2) 2
(3) 0
(4) 1
댓글 수: 0
채택된 답변
dpb
2016년 9월 26일
>> x = [ 0 1 1 0 ; 1 0 0 1 ; 0 0 0 1 ; 1 1 0 0]
>> [[1:4].' sum(x.*x.',2)]
ans =
1 1
2 2
3 0
4 1
>>
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!