How to write to make a computation grid with random numbers?

5 visualizzazioni (ultimi 30 giorni)
I have a grid where -9 ≤x≤9, -8≤y≤8, step size of 0.2.
In order to find all the local minima contained within this computational grid, for a given function, I want to run 150 Gradient Descent searches (for which I have written a function already) using randomised starting points
I know I need to use rand to populate x and y with valuesbetween 0 and 1,but I don't know how to do this and scale up these values to the limits of the numerical grid -9 ≤x≤9, -8≤y≤8, step size of 0.2.
Would anyone be able to explain how i can make this grid?
Thanks in advance!

Risposta accettata

Stephan
Stephan il 3 Gen 2019
Modificato: Stephan il 3 Gen 2019
Hi,
if your function that uses the randomized values is vectorized, you can use this code:
x = -9:0.2:9;
y = -8:0.2:8;
id_x = randi(numel(x),150,1);
id_y = randi(numel(y),150,1);
values = [x(id_x); y(id_y)]';
This produces a 150x2 matrix containing random pairs of x values between -9:0.2:9 in the first column and y values between -8:0.2:8 in column 2.
If you did not vectorize your function you can loop through this matrix to perform your calculations.
Best regards
Stephan

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by