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

1 visualización (últimos 30 días)
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

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by