reproduce random numbers for the third dimension
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Pooneh Shah Malekpoor
el 21 de Abr. de 2021
Respondida: James Tursa
el 21 de Abr. de 2021
Hello
I want to generate random numbers for matrix A which has a second page(i.e. 2), whose values i am going to assign to another varaiable in my code. Though i have the rng command to reproduce the same random numbers, the very numbers in the second page do vary in every iteration..How can I regenerate the same random numbers in each iteration for the second page of my matrix A? Any idea is highly appreciated.
rng('default');
rng(1);
Nmc=200
uu=1
A=rand(Nmc,uu+1,2);
4 comentarios
the cyclist
el 21 de Abr. de 2021
If I understand what you want to do correctly, it is quite simple.
% Set the seed
rng default
% Set the parameters. (I chose small Nmc for illustration)
Nmc=3;
uu=1;
% Preallocate memory for the whole array.
% (Probably only important if you are going to create many slices, or large arrays.)
A = zeros(Nmc,uu+1,2);
% Generate the first slice at random
A(:,:,1) = rand(Nmc,uu+1,1);
% Repeat the first set as the second
A(:,:,2) = A(:,:,1);
% Show the result
disp(A)
Respuesta aceptada
James Tursa
el 21 de Abr. de 2021
If you don't want the random numbers to change from iteration to iteration, then don't regenerate them inside the loop. Calculate them once, before the loop starts, and then reuse them over and over within the loop.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!