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일
a = [5,5,5,5,5];
out=a+randi([-1 1],size(a))

댓글 수: 3

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 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 Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 2월 23일

편집:

2016년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by