Matrix Randomize with order

1 view (last 30 days)
hikmat shakally
hikmat shakally on 17 Mar 2019
Commented: hikmat shakally on 19 Mar 2019
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
madhan ravi
madhan ravi on 17 Mar 2019
what have you tried so far?
hikmat shakally
hikmat shakally on 17 Mar 2019
I couldn't deal with the matrix due to the not equal elements presence, such as 1 has been repeated 50 times and 3 has been repeated 86 times

Sign in to comment.

Answers (2)

Jan
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
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.
hikmat shakally
hikmat shakally on 19 Mar 2019
I am sorry if the explanation wasn't that clear.
What you said below is correct and this is the goal of my problem
"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?" But I didn't understand what do you mean by 8 connected elements.
prefferred solution: no equal neighboring elements in rows, columns and diagonals.
IF IT CAN"T BE
it's ok to have at maximum two equal neighbors (in either rows,or columns, or diagonals)
12354
15432 it's ok! (1 has only one equal neighbor and 3 too has only one equal neighbor)
but
323
231 is not (3 here has more than one eqal neighbor)
Thank you again in advanced!

Sign in to comment.


Walter Roberson
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.

Categories

Find more on Programming 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!