How can I create a random matrix without repeating any value between column
Ältere Kommentare anzeigen
I get two columns with the same value when I generate the matrix. For example,
1 1 3 2 4
4 4 1 4 3
2 2 2 3 1
3 3 4 1 2
I want to get something like the 10 combinations from the 24 combinations(4 factorial).
1 1 3 2 4 1 2 4 3 2
2 4 1 4 3 3 3 1 2 1
3 2 2 3 1 4 1 3 4 3
4 3 4 1 2 2 4 2 1 4
I need the matrix to form the chromosomes for GA.
3 Kommentare
Steven Lord
am 23 Mai 2023
It is not at all clear to me how you obtained the second of those matrices from the first. Some of the columns (like columns 2, 3, 4, and 5) are copies of columns from the first matrix, but some (like columns 1 and 6) are not. What rules specifically do you need to follow in generating that second matrix from the first?
Dyuman Joshi
am 23 Mai 2023
As Steven mentions, it's not clear what is the pattern you are following to obtain the output.
From a cursory glance, it can be observed from the output that the 1st column is sorted and the 6th column corresponds to the indices of the sorted values of the 1st column. But the it's not clear what's the logic behind the columns 7-10.
Wan
am 28 Mai 2023
Akzeptierte Antwort
Weitere Antworten (1)
You can use reshape(), randperm(), numel(), size() fcns to create ramdom swaps of the already existing matrix elements:
A1 = [1 1 3 2 4;
4 4 1 4 3;
2 2 2 3 1;
3 3 4 1 2];
A_new = [reshape(A1(randperm(numel(A1))), size(A1)), reshape(A1(randperm(numel(A1))), size(A1))]
2 Kommentare
Dyuman Joshi
am 23 Mai 2023
@Sulaymon Eshkabilov, every column must contain all the numbers [1 2 3 4]. The output from your code does not.
Wan
am 28 Mai 2023
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!