1

Let's say I have the following matrix

arr1[0,$i] = 0,0,0,0,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3
arr1[1,$i] = 0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,4,4,4,5,5
arr1[2,$i] = 0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,2,2,2
arr1[3,$i] = 0,0,1,1,2,2,3,3,3,4,4,4,5,5,5,6,6,6,6,6

I want to obtain a matrix that has the same number of rows and columns as the original one but with randomized elements that fulfils the following 2 conditions:

  1. Each row of arr2 has to heave the same elements as arr1 but in a random order.
  2. All columns of arr2 should be unique.

Example:

arr2[0,$i] = 0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,1,2,3,2
arr2[1,$i] = 0,1,2,3,4,5,0,1,2,3,4,5,0,1,2,3,4,1,4,1
arr2[2,$i] = 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,2,0,2,2
arr2[3,$i] = 0,1,2,3,4,5,6,0,1,2,3,4,5,6,3,4,5,6,6,6

All I have managed until now was to grow arr1 2 or 3 times, randomize each line, start writing to arr2 column by column and check if the column is unique, if not, just go to the next column in the grown array until I have enough columns to fill arr2. This is not a solution since it does not fulfill condition 1, exactly. It is somehow close but not perfect.

If it is not possible to fulfill condition 2, an error should be displayed.

P.S.: This is an oversimplification of the actual script, putting it all here would just create confusion. If I can figure a way of doing this with your help, I can adapt it to my script. The values in arr2 are actually used as inputs for another array

arr3[$j,arr[0,$i]]
Flo Rentin
  • 11
  • 2
  • 2
    Welcome to StackOverflow. Please show us what you have tried yourself. Note this is *not* a script factory. You will need to write the script yourself. If you run into a problem that isn't yet covered by the existing questions and answers, you might post your [mcve] here so that we might be able to help you further. See also: [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – iRon Feb 05 '22 at 12:48
  • 1
    What should happen if condition #2 can't be satisfied for a given set of inputs? – Mathias R. Jessen Feb 05 '22 at 15:36

0 Answers0