could anyone help me how to generate some of the similar values using rng
조회 수: 2 (최근 30일)
이전 댓글 표시
If i execute the code some both x and y remains same
s = rng
x = rand(1,5)
rng(s)
y=rand(1,5)
But i want three values to be same and the remaining two values to be different.
Could anyone please help me on this.
댓글 수: 0
채택된 답변
the cyclist
2021년 7월 6일
Here is a new answer, based on the comments on the other answers.
numberTotal = 8; % You can change this to 1000
numberRepeat = 3; % You can change this to 200
% rng default
% Generate two full vectors of independent randoms
x = rand(1,numberTotal);
y = rand(1,numberTotal);
% Generate random, non-repeated indices to copy some locations from x to y
indexRepeat = randperm(numberTotal,numberRepeat);
% Overwrite the leading y values from x.
y(indexRepeat) = x(indexRepeat);
x
y
This code will replace a fixed number of elements, but the positions that are replaced will be random. If you need to replace a random number of elements, then use something like
numberRepeat = randi([3,5]);
which will repeat 3, 4, or 5 elements.
댓글 수: 0
추가 답변 (2개)
the cyclist
2021년 7월 5일
Is this what you mean?
rng default % for reproducibility, if desired
[repmat(rand(),1,3) rand(1,2)]
the cyclist
2021년 7월 5일
In response to the comment in my other answer, here is a pretty simple way to do it:
numberRep = 3; % You can change this to 800
numberNew = 2; % You can change this to 200
rng default
% Generate two full vectors of independent randoms
x = rand(1,numberRep + numberNew);
y = rand(1,numberRep + numberNew);
% Overwrite the leading y values from x.
y(1:numberRep) = x(1:numberRep);
Until the number you need gets really large, generating a few spurious numbers in y and overwriting them doesn't matter.
댓글 수: 5
the cyclist
2021년 7월 6일
You did not answer any of the questions I asked in my previous comments.
How do you know which elements to copy? Do you have a fixed list of positions, such as ...
copyList = [3 6 7];
Or do you choose which elements to copy at random?
How many elements should be copied? 100? 200? Half the length of the vector?
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!