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);

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 2 月 23 日

1 投票

a = [5,5,5,5,5];
out=a+randi([-1 1],size(a))

3 件のコメント

Daniel Blair
Daniel Blair 2016 年 2 月 23 日
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].
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 2 月 23 日
Your question is not clear
Daniel Blair
Daniel Blair 2016 年 2 月 23 日
編集済み: Daniel Blair 2016 年 2 月 23 日
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])

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by