How can I randomize elements in an array by a set amount?

For example, if i had the array
a = (5,5,5,5,5);
and I wanted to randomize some of the elements in the array by 1 so that it would look like
a = (5,6,5,5,4);

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 23 Feb. 2016
a = [5,5,5,5,5];
out=a+randi([-1 1],size(a))

3 Kommentare

This works!
Maybe this is asking for too much, but once the randomization process has occured, i.e a = [5,5,5,5,5] has become [5,6,5,5,4], is it possible for the program to start with the last modified array? In this case, the next time it would run would be turning a = [5,6,5,5,4] into possibly [6,7,5,4,3].
Your question is not clear
Daniel Blair
Daniel Blair am 23 Feb. 2016
Bearbeitet: Daniel Blair am 23 Feb. 2016
Okay, lets say I run the code starting with a = [5,5,5,5,5], and then it randomizes some of the elements by adding or subtracting 1 to it. The array called a is now [6,5,5,4,4].
The next time I run the code, I want the program to remember that the array is now [6,5,5,4,4] before randomizing some of its elements by adding or subtracting 1 to them.
(This would mean that after a few executions of the program, the array could look like this: [2,7,4,5,6])

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by