I have a 4x4 array of 1's and I need to turn 3 of 4 ones to zero per row at random. So there will be a one 1 left in each row.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jesse Brimeyer
el 8 de Mayo de 2022
Comentada: Jan
el 9 de Mayo de 2022
A = [1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1];
B = [1 0 0 0; 0 0 1 0; 0 1 0 0; 0 0 0 1]; % An example
2 comentarios
Image Analyst
el 8 de Mayo de 2022
OK, thanks (I guess) for the announcement. Good luck with it. Have fun. 🙂
Torsten
el 8 de Mayo de 2022
Better start with a zero matrix and replace one of the four zeros by a one in each row.
Respuesta aceptada
Más respuestas (1)
the cyclist
el 8 de Mayo de 2022
Here is one way:
N = 4;
x = (1:N)';
y = randi(N,N,1);
linearIndex = sub2ind([N,N],x,y);
B = zeros(N,N);
B(linearIndex) = 1
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!