配列内における特定の行(または列)の出現回数を求める
11 views (last 30 days)
Show older comments
0,1が19×10000格納されているデータがあります。
0 0 0
1 1 1
1 1 1
1 1 0
1 1 1
1 1 1
1 1 1
0 0 0
0 0 0 …
0 0 0
0 0 0
1 1 1
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
0 0 0
行ごとの0,1パターンが、この配列で何回出現しているかカウントしたいです。
上記の例
[0 1 1 1 1 1 1 0 0 0 0 1 0 0 1 1 1 0] が2回
[0 1 1 0 1 1 1 0 0 0 0 1 0 0 1 1 1 0] が1回
良い解決策をお持ちの方、ご意見をお聞かせいただけると幸いです。
0 Comments
Accepted Answer
Atsushi Ueno
on 18 Jan 2023
Edited: Atsushi Ueno
on 19 Jan 2023
>良い解決策をお持ちの方、ご意見をお聞かせいただけると幸いです
行ごとの0,1パターンを10進数に変換し一つの値として捉えるのはどうでしょうか?
bits = 19; % 20230109追記:ビット数を即値⇒変数化
dat = randi(2,[bits,10000]) - 1; % 0,1が19×10000格納されているデータ
dec = bin2dec(num2str(dat')); % 19桁の2進数とみなし、一旦10進数に変換する
N = histcounts(dec,2^bits); % histcounts関数で各パターンの出現回数をカウントする
%for k = 1:numel(bin) % 要件違いの為削除
% disp([dec2bin(bin(k-1)) 'が' num2str(N(bin(k))) '回']);
%end
for k = 1:2^bits % histcountのビン数だけ表示する
disp([dec2bin(k-1) 'が' num2str(N(k)) '回']);
end
4 Comments
Atsushi Ueno
on 19 Jan 2023
もう一つバグがありました。気付かれない様にしれっと直しました。
【誤】
disp([dec2bin(bin(k)) 'が' num2str(N(bin(k))) '回']);
【正】
disp([dec2bin(bin(k-1)) 'が' num2str(N(bin(k))) '回']);
More Answers (0)
See Also
Categories
Find more on マルチレート信号処理 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!