How can I create a random matrix without repeating any value between column
显示 更早的评论
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 个评论
Steven Lord
2023-5-23
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
2023-5-23
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
2023-5-28
采纳的回答
更多回答(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 个评论
Dyuman Joshi
2023-5-23
@Sulaymon Eshkabilov, every column must contain all the numbers [1 2 3 4]. The output from your code does not.
Wan
2023-5-28
类别
在 帮助中心 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!