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!
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Hamming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!