counting column bits with the same weight in binary array

1 回表示 (過去 30 日間)
Henry Buck
Henry Buck 2015 年 9 月 7 日
コメント済み: Henry Buck 2015 年 9 月 9 日
I have a binary array and I want to count the column with the same weight.
I want to do it with function...m-file
I hope that someone can help me to solve it.
Thanks Henry
  3 件のコメント
Henry Buck
Henry Buck 2015 年 9 月 8 日
編集済み: dpb 2015 年 9 月 8 日
For example:
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0
0 1 0 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 0 1
1 0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1
0 0 1 0 0 1 0 1 0 1 0 0 0 1 0 0 1 0 1 1
1 1 0 0 1 0 0 0 1 0 1 1 1 1 0 0 0 0 0 1
0 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0
0 0 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 1 1 0
There is 6 col with the same weight (two 1 bits) - col=0,3,6,7,11,15.
There is 8 col with the same weight (three 1 bits) - col=1,9,10,12,13,14,17,18.
There is 5 col with the same weight (four 1 bits) - col=2,4,5,16,19.
dpb
dpb 2015 年 9 月 8 日
>> [0:size(a,2)-1;sum(a)]
ans =
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 4 2 4 4 2 2 2 3 3 2 3 3 3 2 4 3 3 4
>>
So, turns out not as far off as thought if you count from the left rather than using the header row as the column number. Other than missing one '2' (col 8), seems ok...

サインインしてコメントする。

採用された回答

dpb
dpb 2015 年 9 月 7 日
編集済み: dpb 2015 年 9 月 8 日
n=hist(sum(array),0:size(a,1));
N th element of n will contain counts for that number of bits from zero to size(array,1); ie, for all possible totals from no bits set to all.
ADDENDUM
>> [0:size(a,1);hist(sum(a),0:size(a,1))]
ans =
0 1 2 3 4 5 6 7
0 0 7 8 5 0 0 0
>> sum(ans(2,:))==size(a,2) % sanity check...
ans =
1
>>
Total number does equal number of columns in array...
Actually, there are 7,8,5 of wt=2,3,4, respectively.
  3 件のコメント
dpb
dpb 2015 年 9 月 8 日
If it's a subset of rows, only the array indices to refer to the subset...
r1=3; r2=6;
n=hist(sum(array(r1:r2,:)),0:size(a,1));
Henry Buck
Henry Buck 2015 年 9 月 9 日
Hi dpb, It works perfectly. Thanks for your help, Henry

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by