# could anyone help me how to generate some of the similar values using rng

1 回表示 (過去 30 日間)
jaah navi 2021 年 7 月 5 日

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.

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

### 採用された回答

the cyclist 2021 年 7 月 6 日
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
x = 1×8
0.5267 0.0600 0.3075 0.2606 0.7846 0.8719 0.3408 0.3655
y
y = 1×8
0.4232 0.0600 0.3023 0.5271 0.7846 0.2124 0.3408 0.3699
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.

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

### その他の回答 (2 件)

the cyclist 2021 年 7 月 5 日
Is this what you mean?
rng default % for reproducibility, if desired
[repmat(rand(),1,3) rand(1,2)]
ans = 1×5
0.8147 0.8147 0.8147 0.9058 0.1270
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
jaah navi 2021 年 7 月 5 日
I want to x and y in the following manner.
x= [0.1419 0.4218 0.9157 0.7922 0.9595]
y= 0.1419 0.4218 0.9157 0.6134 0.8234]
As my x and y size is larger (1,1000) i need around 800 values in x should remain same as in y and the rest 200 values can be different.

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

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);
x = 1×5
0.8147 0.9058 0.1270 0.9134 0.6324
y = 1×5
0.8147 0.9058 0.1270 0.9575 0.9649
Until the number you need gets really large, generating a few spurious numbers in y and overwriting them doesn't matter.
##### 5 件のコメント3 件の古いコメントを表示3 件の古いコメントを非表示
the cyclist 2021 年 7 月 6 日
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?
jaah navi 2021 年 7 月 6 日
I need to copy a random number of elements.
The copied elements position should remain the same in both matrices.
Majority of the randomly copied elements should be same (90/100).

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

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by