I want to create a nxn matrix with all 0s and n randomly placed 1s

4 vues (au cours des 30 derniers jours)
Sahil Bajaj
Sahil Bajaj le 26 Nov 2015
Commenté : Sahil Bajaj le 26 Nov 2015
I want to create a nxn zero matrix with n randomly placed ones in the matrix
Example
1st try
1 0 0
0 1 0
0 0 1
2nd Try
0 0 1
0 1 1
0 0 0

Réponse acceptée

James Tursa
James Tursa le 26 Nov 2015
n = whatever;
x = zeros(n);
x(randperm(n*n,n)) = 1;

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by