Borrar filtros
Borrar filtros

How do i assign a random element of an array a new value

2 visualizaciones (últimos 30 días)
Ross Anderson
Ross Anderson el 31 de Oct. de 2018
Editada: madhan ravi el 1 de Nov. de 2018
I have an array maze=strings(size) where all the elements are "0" How did i randomly choose one of these elements and assign it "P"

Respuestas (2)

madhan ravi
madhan ravi el 31 de Oct. de 2018
Editada: madhan ravi el 1 de Nov. de 2018
a=maze;
a(randi([1,numel(a)],1,1))='p'
  2 comentarios
madhan ravi
madhan ravi el 31 de Oct. de 2018
The above shows an example to select random value and replacing it as another.
madhan ravi
madhan ravi el 31 de Oct. de 2018
You can adapt the same way to your case.

Iniciar sesión para comentar.


Stephan
Stephan el 1 de Nov. de 2018
Hi,
try:
maze(randi(numel(maze))) = 'P';
Best regards
Stephan

Categorías

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

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by