How to remove barcodes of Hamming Weight 0 from a Matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Neo Jun Hao
le 21 Mai 2020
Réponse apportée : Raunak Gupta
le 28 Mai 2020
I have a matrix of barcodes of X bits and Y Hamming Weight, and I would like to remove all barcodes that have Hamming Weight of 0 from the matrix. Assume that barcodes of Hamming Weight 0 can only occur at the end of the matrix.
E.g. matrix
m = [1 0 1 0 1; 1 1 0 0 1; 0 0 0 0 0; 0 0 0 0 0]
After removal, the result should be
1 0 1 0 1
1 1 0 0 1
Here is the current code I have
x = 0;
for i = 1:size(m,1)
if sum(m(i,:)) == 0
break;
else
x = x + 1;
continue;
end
end
m = m(1:x,:)
However, I would like to make the code more efficient. Any help on this would be appreciated!
0 commentaires
Réponse acceptée
Raunak Gupta
le 28 Mai 2020
Hi,
Logical indexing can be helpful here and reduce the amount of code as well removes the for loop that is there. Following will do the same as mentioned in the question. This will also work when there are 0 hamming weight rows in between the matrix.
m = m(sum(m,2)>0,:);
sum(m,2) is summation of elements, row-wise.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Hamming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!