How to create a random binary matrix with equal number of ones in each column?

4 ビュー (過去 30 日間)
Anne
Anne 2011 年 11 月 2 日
コメント済み: Image Analyst 2019 年 1 月 24 日
Hi All,
I want to create a random binary matrix with equal number of ones in each column.
Appreciate if anyone have an idea to implement this in Matlab.
Thanks.
  1 件のコメント
the cyclist
the cyclist 2011 年 11 月 2 日
To avoid folks providing answers, and then you saying, "No, that's not what I meant", can you please provide more detail? For example, should each column have equal numbers of zeros and ones? What if there are an odd number of rows? Etc.

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

採用された回答

Image Analyst
Image Analyst 2011 年 11 月 2 日
This is how I did it:
% Set up parameters.
rows = 10;
columns = 15;
onesPerColumn = 4;
% Initialize matrix.
m = zeros(rows, columns, 'int32')
for col = 1 : columns
% Get random order of rows.
randRows = randperm(rows);
% Pick out "onesPerColumn" rows that will be set to 1.
rowsWithOne = randRows(1:onesPerColumn);
% Set those rows only to 1 for this column.
m(rowsWithOne, col) = 1;
end
% Display m
m
  3 件のコメント
Raghwan Chitranshu
Raghwan Chitranshu 2019 年 1 月 24 日
hello,
How if I also want to keep number of ones in rows to be equal.
what changes shall I perform .
can you help in this
Image Analyst
Image Analyst 2019 年 1 月 24 日
It seems like that was solved in this question

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

その他の回答 (4 件)

Sven
Sven 2011 年 11 月 2 日
Try:
% Define your matrix size and randomly pick the number of ones
matSz = [20, 40];
numOnes = randi(matSz(1));
% Make your matrix
myMat = false(matSz);
for i = 1:matSz(2)
myMat(randperm(matSz(1),numOnes), i) = true;
end
% Check that all went as planned
sum(myMat,1)==numOnes
  2 件のコメント
Anne
Anne 2011 年 11 月 2 日
I tried this method, but could not get what I was expecting...
Sven
Sven 2011 年 11 月 2 日
Really? The variable "myMat" is your answer. The last line that prints out a series of ones was just confirmation that all of your columns had "numOnes" true elements in them.
Setting
matSz = [10, 15];
and
numOnes = 4;
gives the exact same output as what you agreed with below.

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


Naz
Naz 2011 年 11 月 2 日
Since it's a RANDOM matrix, you are not guaranteed to have the same amount of one's and zero's (at least it seems logical to me). In order to get about 1/2 probability you need large matrix. You can try this:
a=rand(1000,1000);
a=round(a);
Check out help file for rand vs. randn

Anne
Anne 2011 年 11 月 2 日
I also need to make sure that this matrix is invertible.
What I currently do is check det(A)=0 & rank(A)<3 using a while loop. But sometimes the while loop runs infinitely and the script does not respond.
Is there any other way to check for non-singular matrices?
Thanks again...

Walter Roberson
Walter Roberson 2011 年 11 月 2 日
Anne, you need to define what it means to take an inverse for you binary matrix. You can treat the binary matrix as being composed of the real numbers 0 and 1 and then do an arithmetic inverse on the array, ending up with a non-binary array. Or you can treat the binary matrix as being composed of boolean values over a field with the '*' being equivalent to 'or' and '+' being equivalent to xor, and the task is then to find a second binary matrix such that matrix multiplication using those operations produces the identity matrix.
If you want the inverse to be a binary matrix instead of a real-valued matrix, please see this earlier Question:
  1 件のコメント
Anne
Anne 2011 年 11 月 3 日
I was able to figure this out. Now I'm using GF to find the inverse and to check for invertibility I'm using rank(gf(A))=n
this seems to work...
thanks for your answer...

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by