## Matrix Randomize with order

### hikmat shakally (view profile)

on 17 Mar 2019
Latest activity Commented on by hikmat shakally

### hikmat shakally (view profile)

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;
]

on 17 Mar 2019
what have you tried so far?
hikmat shakally

### hikmat shakally (view profile)

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

on 18 Mar 2019
Edited by Jan

### Jan (view profile)

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]);
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?

hikmat shakally

### hikmat shakally (view profile)

on 18 Mar 2019
while I am compiling the code you commented, I am facing the following Error.
More explaining for the problem:
• If we are able to make the matrix such as for each element no other equal element arround. it would be perfect.
Example:
12345
45123
32451
45132
13245 (no similar elements neither at the same diyagonal nor at the same column or row)
• If it can't be (we are obliged to use equal elements near each other) it would be ok! but at maximum TWO equal elements near each other (in the same diyagonal, column or row)
Example:
12345
31425
42531
32451
Thanks again!
Error using reshape
To RESHAPE the number of elements must not change.
Error in Untitled26 (line 24)
X = reshape(V(index), 17, 21);
Jan

### Jan (view profile)

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 (view profile)

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)