Matlab中的整数列表的汉明权重 [英] Hamming weight for a list of integers in Matlab
本文介绍了Matlab中的整数列表的汉明权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个简单的问题:我有一个整数列表,例如,
Quite a simple problem: I have a list of integers, e.g.,
a = [7 8]
现在我想要一个单独的列表,其中包含汉明重量(即汉明重量) 列表中每个整数的二进制表示形式的1位数字).这意味着上面的整数列表的结果应如下所示:
Now I want to have a seperate list, that contains the Hamming Weight (that is the number of 1 bits in the binary represenation) for each of the integers in the list. That means the result for the integer list above should look as follows:
res = [3 1]
有人知道我如何快速做到这一点吗?
Anyone an idea how I could do this quickly?
推荐答案
这有点棘手,但可以:
res = sum( dec2bin(a).' == '1' );
它将a
转换为二进制表示形式,查看该表示形式中有多少字符是'1'
,然后将这些数字相加.
It converts a
to binary representation, looks at how many characters in that representation are '1'
, and sums up those numbers.
这篇关于Matlab中的整数列表的汉明权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文