octave - How to calculate the Hamming weight for a vector? -


i trying calculate hamming weight of vector in matlab.

function hamming_weight (vet_dec) ham_weight = sum(dec2bin(vet_dec) == '1')     endfunction 

the vector is:

hamming_weight ([208    15   217   252   128    35    50   252   209   120    97   140   235   220    32   251]) 

however, gives following result, not want:

ham_weight =     10   10    9    9    9    5    5    7 

i grateful if me please.

you summing on wrong dimension!

sum(dec2bin(vet_dec) == '1',2).' ans =    3   4   5   6   1   3   3   6   4   4   3   3   6   5   1   7 

dec2bin(vet_dec) creates matrix this:

11010000 00001111 11011001 11111100 10000000 00100011 00110010 11111100 11010001 01111000 01100001 10001100 11101011 11011100 00100000 11111011 

as can see, you're interested in sum of each row, not each column. use second input argument sum(x, 2), specifies dimension want sum along.

note approach horribly slow, can see this question.

edit

for valid, , meaningful matlab function, must change function definition bit.

function ham_weight = hamming_weight(vector)     % return variable ham_weight   ham_weight = sum(dec2bin(vector) == '1', 2).';   % don't transpose if                                                   % want column vector end                                              % endfunction not matlab command. 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -