Shiran Maskit
Shiran Maskit

Reputation: 19

How to generate a random matrix in matlab with each value reapating twice?

i need to make a random matrix, lets say size 3X4, where the values will be between 1-6, and each value will be reapeted exactly twice (in a random location in the matrix). how can i do that? thanks

Upvotes: 0

Views: 213

Answers (1)

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

You can create an array that contains the numbers 1 to 6 with required repition, say

A=[1:6, 1:6];

Then shuffle that array using randperm and reshape it to a 3 by 4 matrix

>> B=reshape(A(randperm(12)), 3, 4)
B =
     5     4     6     1
     2     3     5     4
     6     2     1     3

Upvotes: 9

Related Questions