How can I create a random matrix with no duplicate value in the line

7 views (last 30 days)
I want to create a random matrix with no duplicate value in every line of the matrix

Accepted Answer

Stephen23
Stephen23 on 7 Dec 2016
Edited: Stephen23 on 8 Dec 2016
Use randperm to get a sequence of integers with no repetitions:
>> randperm(9)
ans =
9 6 5 8 3 7 1 4 2
If you want a matrix where each row has unique numbers, then you can use a loop:
R = 3;
C = 5;
mat = NaN(R,C);
for k = 1:R
mat(k,:) = randperm(C);
end
which gives:
>> mat
mat =
3 4 1 5 2
4 1 2 5 3
4 5 2 1 3

More Answers (1)

KSSV
KSSV on 7 Dec 2016
doc rand..
K = rand(10,7);

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!