We have the following matrix result
:
result =
Columns 1 through 13
3 1 1 1 1 1 6 2 3 6 2 1 6
4 3 3 5 7 5 10 10 4 10 6 9 8
6 4 4 7 9 7 0 0 0 0 0 0 0
10 5 5 8 0 0 0 0 0 0 0 0 0
Columns 14 through 25
2 10 3 10 3 8 8 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Its column unique element index size is (without zeros):
Indexes of result:
Columns 1 through 13
4 4 4 4 3 3 2 2 2 2 2 2 2
Columns 14 through 25
2 1 1 1 1 1 1
I want to perform the following scenario: Starting from the first column we want to restrict each non-unique value to be present only once in our matrix. So with col1 as starting point the rest of matrix should be rearranged as:
result =
Columns 1 through 13
3 1 1 1 1 1 0 2 0 0 2 1 0
4 0 0 5 7 5 0 0 0 0 0 9 8
6 0 0 7 9 7 0 0 0 0 0 0 0
10 5 5 8 0 0 0 0 0 0 0 0 0
Columns 14 through 25
2 0 0 0 0 8 8 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Indexes of result (without zeros):
Columns 1 through 13
4 2 2 4 3 3 0 1 0 0 1 2 1
Columns 14 through 25
2 0 0 0 0 1 1
Now as we see col4 has the most unique elements, so we consider its values to continue to second re-arrangement and the result is:
result =
Columns 1 through 13
3 0 0 1 0 0 0 2 0 0 2 0 0
4 0 0 5 0 0 0 0 0 0 0 9 0
6 0 0 7 9 0 0 0 0 0 0 0 0
10 0 0 8 0 0 0 0 0 0 0 0 0
Columns 14 through 25
2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Indexes of result (without zeros):
Columns 1 through 13
4 0 0 4 1 0 0 1 0 0 1 1 0
Columns 14 through 25
1 0 0 0 0 1 1
Doing that as many times as necessary, in that example twice more for col5 and col8 we reach the desired result:
result =
Columns 1 through 13
3 0 0 1 0 0 0 2 0 0 0 0 0
4 0 0 5 0 0 0 0 0 0 0 0 0
6 0 0 7 9 0 0 0 0 0 0 0 0
10 0 0 8 0 0 0 0 0 0 0 0 0
Columns 14 through 25
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Indexes of result (without zeros):
Columns 1 through 13
4 0 0 4 1 0 0 1 0 0 0 0 0
Columns 14 through 25
0 0 0 0 0 0 0
Which is the most efficient way to perform this? May I see your suggestions please ?
Thank you in advance.