Matrix Randomize with order
1 view (last 30 days)
Show older comments
Hello!,
I have a matrix consists of 357 elements 17*21
The 357 matrix's elemetns are 1,2,3,4,5 and the repeating times for every one like the following way (1*50 , 2*50 , 3*86 , 4*86 , 5*85 = 357 elements)
How can I shuffle these elements in the matrix regarding to one of the below options:
1-Either no similar elements at the same diyagonal (or line), for example if there is '2' no other '2' would be around it.
2-Or at maximum two equal element could be at the same diyagonal (or line).
A = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2;
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2;
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3;
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3;
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3;
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3;
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4;
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5;
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5;
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5;
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5;
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5;
]
2 Comments
Answers (2)
Jan
on 18 Mar 2019
Edited: Jan
on 19 Mar 2019
Maybe a trial and error approach is working. Start with creating the data:
V = repelem(1:5, [50, 50, 86, 86, 85]);
ready = false;
while ~ready
index = randperm(numel(V), numel(V)); % [EDITED: Typo fixed, X->V]
X = reshape(V(index), 17, 21);
% Now check the conditions and set ready to true on demand
end
Unfortunately I cannot suggest code for the check, because this is not clear:
no similar elements at the same diyagonal (or line), for example if there is '2' no other '2' would be around it
What does this exactly mean? Do the neighboring elements in the columns matter or just the diagonals and rows?
3 Comments
Jan
on 19 Mar 2019
Edited: Jan
on 19 Mar 2019
I had a typo in my code. See [EDITED].
What does "near to eachother" exactly mean? Do you mean direct neighbors?
My approach creates a random permutation. Then you can insert the code to compare the neighboring elements (just 2 for loops and some comparisons - please try this by your own, it is not hard). The random shots have the disadvantage, that it cannot detect if there is no possible solution. It will run forever, or until a certain limit of iterations. Unfortunately "If it can't be" cannot be caught.
As far as I understand, you want to try to create the matrix with no equal neighboring elements (in rows, columns and diagonals: The "8 connected elements"). But if such a matrix does not exist, up to 2 equal neighbors are accepted also. But does this mean "two equal" per element, or totally over all elements?
The description of the problem is still not unique. It would be useful, if you explain the background of the problem. Perhaps you have discussed global optimization problems in the university, or a "bombing method" for solving the travelling salesman problem. Maybe you want a brute force attack, or a constructive solution. Please elaborate the explanations, until they are unique and clear.
Walter Roberson
on 18 Mar 2019
There cannot be any solution to this problem.
Consider that you have 21 columns. Each "line" (including columns) can have a maximum of 2 copies of any particular element. Therefore a maximum of 2*21 = 42 entries can be occupied by 1s. But you have 50 ones. Therefore a minimum of 8 columns need to have 3 1's.
You have 86 3's. That is enough to fill up 21 columns 4 times over with 2 left over, so there will have to be at least 2 columns that have 5 3's. And with there being 17 rows, there will have to be at least one row that has 6 3's.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!