Can we Generate a Random Matrix with No Repeated Elements

I'm aware of randperm, but it gives vectors rather than 2D matrices. One may also use a loop with randperm to generate a random matrix with no repeated elements in each row, but overalll, this doesn't guarantee the non-redundancy of any element if we consider the whole matrix.
Is there a way to do that?

 Respuesta aceptada

Just the reshape long vector returned by randperm
m = 3;
n = 2;
A = reshape(randperm(10,m*n), m, n)
A = 3×2
7 10 1 4 2 6

3 comentarios

Nice work. Can we choose the integers from a sample interval instead of starting from 1?
You can map them to anything you like by simple indexing, here is set s as example:
m = 3;
n = 2;
s = 101:110;
A = s(reshape(randperm(numel(s),m*n), m, n))
A = 3×2
109 107 105 104 110 102
Fantastic!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Random Number Generation en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

el 8 de Nov. de 2022

Comentada:

el 8 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by