Generate Random Matrix 0's,1's

조회 수: 27 (최근 30일)
Mohammad Al ja'idi
Mohammad Al ja'idi 2019년 6월 13일
댓글: Mohammad Al ja'idi 2019년 6월 14일
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
John D'Errico
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
Mohammad Al ja'idi 2019년 6월 13일
편집: Mohammad Al ja'idi 2019년 6월 13일
Hello mr. John D'Errico
yes i need only some of them at random distribution for 1's which each row has only one (1) and the rest are 0's, this is my code which I suppose 10 random matrices, but the problem that the 1's are always on the same column, how can i make the 1's distributed randomly.
matno = 10; % the number of random possiblities
rows=10;
columns=3;
matrices = zeros(rows,columns,matno);
for i=1:matno
matrices(:,randi(columns),i)=1;
end

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2019년 6월 13일
편집: John D'Errico 2019년 6월 13일
Easy enough.
matno = 10; % the number of random possiblities
rows=10;
columns=3;
Now we want to create a matrix of size (rows,columns,matno), such that each row has exactly one element that is 1, and the placement of that 1 is random. This is most simply done by creating a matrix of size (matno*rows,columns), where each of those rows has exactly one true element.
The location of those 1's will be:
cloc = randi(columns,[matno*rows,1]);
matrices = zeros(matno*rows,columns);
matrices(sub2ind([matno*rows,columns],(1:(matno*rows))',cloc)) = 1;
matrices = permute(reshape(matrices,matno,rows,columns),[2 3 1]);
matrices
matrices(:,:,1) =
0 0 1
0 0 1
1 0 0
0 1 0
0 1 0
1 0 0
1 0 0
0 0 1
0 0 1
1 0 0
matrices(:,:,2) =
0 1 0
1 0 0
1 0 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
1 0 0
0 0 1
matrices(:,:,3) =
1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
1 0 0
1 0 0
0 1 0
0 0 1
1 0 0
matrices(:,:,4) =
0 1 0
0 0 1
0 1 0
0 1 0
0 0 1
0 0 1
0 1 0
0 1 0
0 1 0
1 0 0
matrices(:,:,5) =
1 0 0
1 0 0
0 1 0
0 0 1
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
0 1 0
matrices(:,:,6) =
0 0 1
0 0 1
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
1 0 0
0 0 1
0 1 0
matrices(:,:,7) =
1 0 0
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 0 1
0 0 1
0 1 0
1 0 0
matrices(:,:,8) =
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
1 0 0
0 1 0
0 1 0
0 0 1
matrices(:,:,9) =
0 0 1
1 0 0
0 0 1
1 0 0
1 0 0
0 1 0
0 1 0
0 0 1
0 0 1
1 0 0
matrices(:,:,10) =
1 0 0
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
1 0 0
0 1 0
1 0 0
0 1 0
Easy. Almost trivial. To prove that the result has exactly one 1 in each row, this next result should be a 10x10 array of 1's.
squeeze(sum(matrices,2))
ans =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
As it is.
  댓글 수: 2
Mohammad Al ja'idi
Mohammad Al ja'idi 2019년 6월 14일
Great .. thank you so much
madhan ravi
madhan ravi 2019년 6월 14일
+1

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
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
madhan ravi
madhan ravi 2019년 6월 14일
+1
Mohammad Al ja'idi
Mohammad Al ja'idi 2019년 6월 14일
what about the duplication in the results

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by