Effacer les filtres
Effacer les filtres

how to count identical elements in a non-symmetrical matrix?

2 vues (au cours des 30 derniers jours)
Laszlo Bodnar
Laszlo Bodnar le 26 Sep 2016
Réponse apportée : dpb le 26 Sep 2016
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

Réponse acceptée

dpb
dpb le 26 Sep 2016
>> 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
>>

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by