How to remove barcodes of Hamming Weight 0 from a Matrix

1 次查看(过去 30 天)
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!

采纳的回答

Raunak Gupta
Raunak Gupta 2020-5-28
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by