フィルターのクリア

Binary table - counting bits in rows

4 ビュー (過去 30 日間)
Henry Buck
Henry Buck 2015 年 9 月 15 日
編集済み: Star Strider 2015 年 9 月 15 日
Hi,
How can I print only the rows that have only two '1' bits in rows? like this one:
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
Thanks, Henry

採用された回答

Thorsten
Thorsten 2015 年 9 月 15 日
table= dec2bin(0:(2^4-1)) - '0';
Nones = sum(table, 2);
table(Nones==2, :)
  2 件のコメント
Henry Buck
Henry Buck 2015 年 9 月 15 日
Thanks a lot, Henry
Image Analyst
Image Analyst 2015 年 9 月 15 日
table is a reserved word for a special data type in MATLAB so I would pick another variable name.
When you said "two '1' bits in rows" and gave this "0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0" (just a single row), I thought you meant "two 1's in a row", meaning two 1's that were consecutive/adjacent, so that's what my code did.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 9 月 15 日
Do you have the Image Processing Toolbox? If so, it's simple to just call regionprops on each row and measure the lengths of all 1 segments. If any of the lengths = 2, then print that row with fprintf(), like this untested code:
for row = 1 : rows
thisRow = m(row, :);
labeledData = bwlabel(thisRow); % Identify each separate segment.
measurements = regionprops(labeledData, 'Area');
allLengths = [measurements.Area];
% See if any of the stretches of 1's are 2 in length
if any(allLengths == 2)
fprintf('%d ', thisRow);
fprintf('\n');
end
end
  1 件のコメント
Henry Buck
Henry Buck 2015 年 9 月 15 日
Thanks a lot, Henry

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by