Asked by hikmat shakally
on 17 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;

]

Answer by Jan
on 18 Mar 2019

Edited by 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?

hikmat shakally
on 18 Mar 2019

First of all thank you for your answer.

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

Answer by 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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## madhan ravi (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/450540-matrix-randomize-with-order#comment_682037

## hikmat shakally (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/450540-matrix-randomize-with-order#comment_682039

Sign in to comment.