how can I generate a random matrix?

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. :)

댓글 수: 1

Image Analyst
Image Analyst 2012년 10월 7일
There is a help facility in MATLAB (beyond the Answers forum). It's right there built into MATLAB. For things like this you can use the help documentation. So you can either type "help random" "doc random" into the command window, or click the question mark icon, or (in older versions) get help from the menus. Or you can click in a word in the editor and type F1. For things like this (random) you'd get faster help by using the built-in help.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 10월 7일

1 개 추천

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

댓글 수: 3

jelly
jelly 2012년 10월 7일
hey, I just wanna ask, what if i was given 43 as number limit, how can I make a matrix of m by n? example if m=10 and n=4?
A=randperm(x)
out=reshape(A(1:40),10,4)
Or, if your version of MATLAB supports it:
f = @(x,m,n) reshape(randperm(x,m*n),m,n);
f(43,,10,4)
ans =
40 25 43 16
3 9 11 2
19 36 29 26
37 32 8 27
7 38 12 31
33 41 35 39
22 20 42 1
30 4 24 15
10 17 5 23
28 34 6 21

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2012년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by