Using Randi with min and max matrices
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Anonymous Anonymous
el 30 de Nov. de 2017
Comentada: Anonymous Anonymous
el 4 de Dic. de 2017
While trying to solve a problem I came across the following question (it closely relates to the issue I'm having):
I want to know if this can be done using Randi? One of the answers to that particular question demonstrates a method using Randi; however, it involves a for loop. Is there a way of achieving the same outcome without the for loop?
0 comentarios
Respuesta aceptada
Walter Roberson
el 30 de Nov. de 2017
Assuming two vectors of equal sizes, A (representing minimum values) and B (representing maximum values), and assuming that you want to generate one random value for each entry, then
BAspan = B - A + 1;
span_required = fold( @lcm, BAspan(:).' );
R = randi([0 span_required-1], size(A));
C = A + mod(R, BAspan);
4 comentarios
Walter Roberson
el 30 de Nov. de 2017
If all of the bounds are the same, then sort() the values afterwards (though that in itself leaves open the possibility that values could be equal.)
If the bounds are all the same, consider changing strategy:
BAspan = B(1) - A(1) + 1;
if BAspan < 200; error('Not enough room in those bounds to generate 200 integers'); end
C = sort(A(1) + randperm(BAspan, 200) - 1);
These values are guaranteed to be different.
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!