Matlab 中整数列表的汉明权重
一个非常简单的问题:我有一个整数列表,例如,
a = [7 8]
现在我想要一个单独的列表,其中包含汉明权重(即 列表中每个整数的二进制表示形式中 1 的位数。这意味着上面整数列表的结果应如下所示:
res = [3 1]
有人知道如何快速完成此操作吗?
Quite a simple problem: I have a list of integers, e.g.,
a = [7 8]
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有点 hacky,但它有效:
它将
a
转换为二进制表示形式,查看该表示形式中有多少个字符是'1'
,并对这些数字求和。This is a little hacky, but it works:
It converts
a
to binary representation, looks at how many characters in that representation are'1'
, and sums up those numbers.