Create a matrix with unique coloms and rows
Show older comments
I want to generate a matrix which looks like this:
2 2 3 3 1 1
1 3 2 1 3 2
3 1 1 2 2 3
The specifications of the matrix is:
- A column contains numbers from one to the length of the column.
- Each number occur two times in each row
- Each number occur one time in each column
- No columns are pairwise equal more than one time.
The 3x6 matrix is generated using following code:
post = 3;
team = 6;
A = [];
stop = 0;
number_of_matrix = 0;
unique_matrix = 0;
B = zeros(post,post);
while stop == 0
for i = 1:team
A(:,i) = randsample(post,post); % Genrates a random matrix
end
number_of_matrix = number_of_matrix + 1;
for i = 1:post
B(:,i) = sum(A==i,2);
end
k = 0;
vector = zeros(1,50);
if unique(B) == 2
for m = 1:size(A,2)
for n = 1:size(A,2)
k = k + 1;
if n ~= m
vector(k) = sum(A(:,n) == A(:,m));
end
end
end
end
if unique(B) == 2
if max(vector) <= 1
stop=1;
end
end
end
disp(A)
I want to generate a 6x12 matrix, but this code is very ineffective. I have tried three different approaches, but none of them are faster that this one.
My question is: Does anyone know how to make this code more effective?
Accepted Answer
More Answers (0)
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!