Can anyone help me to generate a matrix of 0's and 1's randomly in which each contains only one 1 like [ 0 1 0 0 0; 1 0 0 0 0; 0 0 0 1 0].
Ältere Kommentare anzeigen
for example
[0 1 0 0 0; 1 0 0 0 0; 0 0 0 1 0]
Akzeptierte Antwort
Weitere Antworten (2)
Shameer Parmar
am 17 Jun. 2016
0 Stimmen
eye(10);
Jos (10584)
am 17 Jun. 2016
One 1 in each ... what? rows, columns, or both?
Given your examples and comment I assume both, so here is a simple one-liner. NR and NC are the number of rows and columns, respectively. Note that NC should be larger or equal to NR
A = full(sparse(1:NR, randperm(NC,NR), 1, NR, NC))
Kategorien
Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!