Matrix with nested for loops

1 visualización (últimos 30 días)
Sophie Stocker
Sophie Stocker el 19 de Jun. de 2020
Comentada: Sophie Stocker el 19 de Jun. de 2020
Hello!
I have to create two 20x20 matrices in which the values should be either 0 or 1 and are randomly distributed.
In one matrix the chance of getting a 1 should be 90% and in the other one it should be 95%.
I don't really understand how the nested for loop works or how the values created in the for loop get assigned to the Matrix.
I would really appreciate your help!
Thanks in advance!

Respuesta aceptada

James Tursa
James Tursa el 19 de Jun. de 2020
You don't need loops for this. E.g.,
p = the probability
result = rand(20,20) < p; % your matrix of 0's and 1's
  1 comentario
Sophie Stocker
Sophie Stocker el 19 de Jun. de 2020
Thank you so much! You saved my day :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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!

Translated by