MATLAB: How can I generate a random matrix

functionrandreshape

hello guys, what code can i use to generate a mxn matrix provided that the value is from 1 to x(depending on the desire of user) and the values should be unique from each other. ๐Ÿ™‚

Best Answer

You will of course have to make sure that x==m*n, as you describe it.
f = @(x,m,n) reshape(randperm(x),m,n);
f(20,5,4)
ans =
4 9 12 2
17 1 8 16
19 7 6 18
5 3 14 13
20 11 15 10