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

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"

Antworten (2)

madhan ravi
madhan ravi am 31 Okt. 2018
Bearbeitet: madhan ravi am 1 Nov. 2018
a=maze;
a(randi([1,numel(a)],1,1))='p'

2 Kommentare

The above shows an example to select random value and replacing it as another.
You can adapt the same way to your case.

Melden Sie sich an, um zu kommentieren.

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

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Tags

Gefragt:

am 31 Okt. 2018

Bearbeitet:

am 1 Nov. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by