Beginner coding; my second, simpler approach at trying to make all elements distinct.

So this is what I've come up with on my own to print a 3x3 random matrix of integers only; no element value exceeding n^2 (9 in this case). I think I've done that correctly, it seems to be working. Keep in mind I've just started learning.
what can I add to ensure that every element will be unique, i.e for this case, all numbers 1-9 should be represented ONCE in the 3x3 matrix; none repeating. As you can see from what I've pasted from the command window, there are two 8's!
If you're willing please explain the thought behind it and exactly how I should approach modifying what's existing. Thanks!
Script
___________
n=3
q=(n^2)*rand(n)
ceil(q)
__________
OUTPUT:
n =
3
q =
7.3798 4.7820 5.4986
6.4652 2.9263 7.0092
8.7178 0.9507 3.8111
ans =
8 5 6
7 3 8
9 1 4

Respuestas (1)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 16 de Feb. de 2022

Respondida:

el 16 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by