Create a matrix with random integers between 1 and 9

10 visualizaciones (últimos 30 días)
Enric Gironès
Enric Gironès el 14 de Mayo de 2019
Editada: Andrei Bobrov el 14 de Mayo de 2019
Hello.
I need help creating a 3x3 matrix.
I need to create a matrix of integers between 1 and 9 allocated randomly each time and without repeating the same number.
Here's an example:
matrix =
9 7 3
5 1 8
4 2 6
Thanks!

Respuesta aceptada

Stephen23
Stephen23 el 14 de Mayo de 2019
Editada: Stephen23 el 14 de Mayo de 2019
>> reshape(randperm(9),3,3)
ans =
6 8 2
3 5 4
7 1 9

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 14 de Mayo de 2019
Editada: Andrei Bobrov el 14 de Mayo de 2019
[~,ii] = sort(rand(9,1));
out = reshape(ii,3,3);

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by