How to put different seed in a multidimensional matrix

1 Ansicht (letzte 30 Tage)
chan
chan am 13 Jul. 2022
Kommentiert: KSSV am 14 Jul. 2022
I have a multidimensional matrix where n=10, k=3
X = rand([n,k,n]);
How to put different seed number for each matrix
X(:,:,1)
X(:,:,2)
X(:,:,3)
.....
.....
X(:,:,10)
Thse seed no. for each matrix should be different.

Akzeptierte Antwort

KSSV
KSSV am 13 Jul. 2022
n = 10; k = 3 ;
R = zeros(n,k,n) ;
for i = 1:n
rng(i) % i can be a positive integer
R(:,:,i) = rand(n,k) ;
end
  2 Kommentare
chan
chan am 13 Jul. 2022
Thank you for giving me the solution but i dnt want the random no. to be repeated for every run. How to do that?
KSSV
KSSV am 14 Jul. 2022
Off course the number are random and they don't repeat.....also note that, there is no ncecessity to set seed for each iteration. You just need:
R = rand(10,3,10) ;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Chunru
Chunru am 13 Jul. 2022
Not sure why you have to do this. But it can be done as follows:
n = 10; k=3;
X = zeros([n,k,n]);
seeds = [1:n]; % your seed numbers
for i=1:n
rng(seeds(i));
X(:, :, i) = rand(n, k);
end
  1 Kommentar
chan
chan am 13 Jul. 2022
Bearbeitet: chan am 13 Jul. 2022
Thank you for giving me the solution but i dnt want the random no. to be repeated for every run. How to do that?

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 13 Jul. 2022
You might think that by changing the seed so frequently you're making the numbers "more random". You're not, as stated by the Note on this documentation page.
Call rng once before you start generating random numbers. Generate your numbers. Then if you need a different seed for your next run call rng again with a different seed.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by