Generate Random Matrix 0's,1's
古いコメントを表示
clould you please assist me to solve the following question:
I need to get random of (0,1) matrices with size (n*m) with the following constraints:
- Each row has only one (1) and the other values are (0's)
as in the following example:
Example :: for matrix (3*2) as we see we have only one (1) in each row and the other columns are 0's
[1 0 0
1 0 0]
---------
[1 0 0
0 1 0]
---------
[1 0 0
0 0 1]
--------
[0 1 0
1 0 0]
----------
[0 1 0
0 1 0]
---------
[0 1 0
0 0 1]
--------
[0 0 1
1 0 0]
--------
[0 0 1
0 1 0]
--------
[0 0 1
0 0 1]
4 件のコメント
KALYAN ACHARJYA
2019 年 6 月 13 日
編集済み: KALYAN ACHARJYA
2019 年 6 月 13 日
I supposed you asked the same question privious, try it yourself, great way of leraning
*Hint
m=3;
n=2;
mat=zeros(m,n);
mat(:,randi(n))=1
Now make the above code for variable cloumn number, not fixed as shown in the above.
Mohammad Al ja'idi
2019 年 6 月 13 日
John D'Errico
2019 年 6 月 13 日
編集済み: John D'Errico
2019 年 6 月 13 日
I MIGHT answer, except your question is inconsistent. Are you looking for random matrices, or ALL POSSIBILITIES? You say both, in different places.
Note that there are m^n possible such matrices, given a matrix sixze of n rows, m columns. Do you want to create all of them? or only some, at random?
Mohammad Al ja'idi
2019 年 6 月 13 日
編集済み: Mohammad Al ja'idi
2019 年 6 月 13 日
採用された回答
その他の回答 (1 件)
Guillaume
2019 年 6 月 13 日
Note that a 3x2 is a matrix with 3 rows and 2 columns, not the other way round.
Bearing in mind that there are n^m such matrices of size m x n, which will quickly get out of hand as n and particularly m grows, here is one way:
m = 4; %number of ROWS. demo data
n = 5; %number of COLUMNS. demo data
columns = cell(1, m); %1 x m array to store all possible column combinations
[columns{:}] = ndgrid(1:n); %cartesian product of 1:n across all rows
columns = cat(m+1, columns{:}); %store as one matrix
pages = repmat(1:n^m, 1, m); %matching page for each element of columns
rows = repelem(1:m, n^m); %matching row for each element of columns
result = zeros(m, n, n^m); %destination matrix
result(sub2ind(size(result), rows(:), columns(:), pages(:))) = 1;
result is a m x n x (n^m) matrix where result(:, :, p) is one of your desired matrix.
Note that you could use Matt J's ndSparse function to build a sparse matrix (using the same rows(:), columns(:), pages(:) arguments) instead of a full matrix. It would be a lot more memory efficient.
5 件のコメント
Mohammad Al ja'idi
2019 年 6 月 13 日
編集済み: Guillaume
2019 年 6 月 13 日
Guillaume
2019 年 6 月 13 日
My code above generates all 59049 possible matrices of size 10x3 in about a 10th of a second on my computer, so it's unclear how it doesn't answer your question.
Once all have been generated you can easily select a subset if you so wish:
%result: matrix generated by above code
matno = 10;
subset = result(:, :, randperm(size(result, 3), matno));
Mohammad Al ja'idi
2019 年 6 月 14 日
madhan ravi
2019 年 6 月 14 日
+1
Mohammad Al ja'idi
2019 年 6 月 14 日
カテゴリ
ヘルプ センター および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!