フィルターのクリア

how do I generate a binary matrix with specified number of 'ones' in each row & column?

7 ビュー (過去 30 日間)
nafila aytija
nafila aytija 2016 年 3 月 24 日
コメント済み: Image Analyst 2016 年 3 月 24 日
Is there any algorithm to do that?
  2 件のコメント
James Tursa
James Tursa 2016 年 3 月 24 日
Please give a small example.
nafila aytija
nafila aytija 2016 年 3 月 24 日
H=[1 1 1 0 0 1 1 0 0 0 1 0; 1 1 1 1 1 0 0 0 0 0 0 1;0 0 0 0 0 1 1 1 0 1 1 1; 1 0 0 1 0 0 0 1 1 1 0 1; 0 1 0 1 1 0 1 1 1 0 0 0; 0 0 1 0 1 1 0 0 1 1 1 0]; Each column has 3 ones and each row has 6 ones....

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

回答 (3 件)

Walter Roberson
Walter Roberson 2016 年 3 月 24 日
Because the number might be 1, this process is at least as difficult as the n- Rooks puzzle, the task of placing n chess rooks on a n x n board so that they cannot attack each other. You can find more information about the better studied n-Queens problem (diagonals have to be paid attention to) here
  3 件のコメント
Roger Stafford
Roger Stafford 2016 年 3 月 24 日
That would be impossible Nafila! Two ones in each column with six columns add up to a total of twelve ones, whereas four ones in each row with four rows add up to a total of sixteen ones. That can't be done.
Walter Roberson
Walter Roberson 2016 年 3 月 24 日
One thing you have not made clear is whether the task is to generate one such matrix at random, or if the task is to generate all such matrices given the parameters of size and number for row and column?

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


KSSV
KSSV 2016 年 3 月 24 日
If you want a matrix where positions of 0, 1 are random. You may try round(rand(m,n)).
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 3 月 24 日
That will often not have the specified number of 1's in each row and column.
nafila aytija
nafila aytija 2016 年 3 月 24 日
yes....random will generate the random number of bits.not the specified ones......

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


Image Analyst
Image Analyst 2016 年 3 月 24 日
I think you might mean this:
% Specify 3 1's in each row.
numOnes = 3
% Create a 10 by 10 matrix of false/0's.
rows = 10;
columns = 15;
binaryMatrix = false(rows, columns);
% Go down row by row finding positions for numOnes 1's and assign them
for row = 1 : rows
% Get locations in this row.
oneLocations = randperm(columns, numOnes)
% Assign true/1 to those columns.
binaryMatrix(row, oneLocations) = true;
end
% Print final result to command window
binaryMatrix
  4 件のコメント
Image Analyst
Image Analyst 2016 年 3 月 24 日
Oh wait, reading your comment to Walter it looks like you need the number in each column also to be a specified number. In that case, this code will not guarantee that since they're scrambled on a row-by-row basis and does not take other rows into account so you could have any number from 0 to rows in any given column, not necessarily the specified number you want.
Image Analyst
Image Analyst 2016 年 3 月 24 日
By the way, for the special case of a square matrix where you want the same number in the rows and columns, you can do that. The concept is called "Latin Square" - you might want to research it.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by