randomly fill 2d matrix with conditions
Question:
Here is my situation and I am just throwing it out there to get ideas how to approach it.
I have categories categories = [0, 1, 3]
and an 10 x 5
matrix, A
How can I fill matrix A with random choices from categories such that each column must have ONE and only ONE occurrence of ‘0’ and ONE and only ONE occurrence of ‘1’ ?
My thought process is:
For each column, Cj,
create list L = selection of random integers between [0,9] ( no replacement)
Set Cj[k[0]] = 0 and Cj[k[1]] = 1
Set Cj[k[i]] , i=2, ..,9
Does anyone have a better way of solving this problem?
Answers:
Your question is a bit vague. Please provide more details on what you can have in other positions. if one position is 0 and 1 position is 1 and the other positions can be whatever, than use this.
make an array[number_of_columns] = {0,1,2,2,….,2], Shuffle it. then for each position in the column (i) if (array[i] = 0){array_you_are_making[i]=0};if(array[i] = 1){array_you_are_making[i]=1};if(array[i] = 2){array_you_are_making[i]="do something"};
Sounds like a case for a Python shuffle() method.
Here’s one way to do it assuming you already have the matrix. (The question says fill a matrix, not create a matrix)
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
newMatrix = []
for row in matrix:
tempRow = restrictedNumbers #first add the restricted numbers since they must appear once
while len(tempRow) < len(row): #keep adding numbers from unrestricted till lengths are equal
tempRow.append(random.choice(unrestrictedNumbers))
random.shuffle(tempRow)
newMatrix.append(tempRow[:]) #finally add the shuffled row to newMatrix
pprint.pprint(newMatrix)
This gives the output: (obviously will vary because of random)
[[3, 1, 3, 0, 3],
[3, 3, 0, 1, 3],
[0, 3, 1, 3, 3],
[0, 3, 1, 3, 3],
[3, 3, 0, 3, 1],
[0, 1, 3, 3, 3],
[1, 0, 3, 3, 3],
[1, 3, 0, 3, 3],
[3, 3, 0, 3, 1],
[3, 0, 1, 3, 3]]
You can see each line has a single 0 and a single 1
Here’s an even shorter way:
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
newMatrix = []
for row in matrix:
row = restrictedNumbers + [random.choice(unrestrictedNumbers) for _ in range(len(row)-len(restrictedNumbers))]
random.shuffle(row)
newMatrix.append(row)
pprint.pprint(newMatrix)
And even shorter than that (this would be one line if random.shuffle returned the list instead of shuffling in place)
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
def returnShuffled(startList):
random.shuffle(startList)
return startList
newMatrix = [returnShuffled(restrictedNumbers + [random.choice(unrestrictedNumbers) for _ in range(len(row) - len(restrictedNumbers))]) for row in matrix]
pprint.pprint(newMatrix)
Here is my situation and I am just throwing it out there to get ideas how to approach it.
I have categories categories = [0, 1, 3]
and an 10 x 5
matrix, A
How can I fill matrix A with random choices from categories such that each column must have ONE and only ONE occurrence of ‘0’ and ONE and only ONE occurrence of ‘1’ ?
My thought process is:
For each column, Cj,
create list L = selection of random integers between [0,9] ( no replacement)
Set Cj[k[0]] = 0 and Cj[k[1]] = 1
Set Cj[k[i]] , i=2, ..,9
Does anyone have a better way of solving this problem?
Your question is a bit vague. Please provide more details on what you can have in other positions. if one position is 0 and 1 position is 1 and the other positions can be whatever, than use this.
make an array[number_of_columns] = {0,1,2,2,….,2], Shuffle it. then for each position in the column (i) if (array[i] = 0){array_you_are_making[i]=0};if(array[i] = 1){array_you_are_making[i]=1};if(array[i] = 2){array_you_are_making[i]="do something"};
Sounds like a case for a Python shuffle() method.
Here’s one way to do it assuming you already have the matrix. (The question says fill a matrix, not create a matrix)
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
newMatrix = []
for row in matrix:
tempRow = restrictedNumbers #first add the restricted numbers since they must appear once
while len(tempRow) < len(row): #keep adding numbers from unrestricted till lengths are equal
tempRow.append(random.choice(unrestrictedNumbers))
random.shuffle(tempRow)
newMatrix.append(tempRow[:]) #finally add the shuffled row to newMatrix
pprint.pprint(newMatrix)
This gives the output: (obviously will vary because of random)
[[3, 1, 3, 0, 3],
[3, 3, 0, 1, 3],
[0, 3, 1, 3, 3],
[0, 3, 1, 3, 3],
[3, 3, 0, 3, 1],
[0, 1, 3, 3, 3],
[1, 0, 3, 3, 3],
[1, 3, 0, 3, 3],
[3, 3, 0, 3, 1],
[3, 0, 1, 3, 3]]
You can see each line has a single 0 and a single 1
Here’s an even shorter way:
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
newMatrix = []
for row in matrix:
row = restrictedNumbers + [random.choice(unrestrictedNumbers) for _ in range(len(row)-len(restrictedNumbers))]
random.shuffle(row)
newMatrix.append(row)
pprint.pprint(newMatrix)
And even shorter than that (this would be one line if random.shuffle returned the list instead of shuffling in place)
import pprint
import random
categories = [0,1,3] #complete pool of numbers
restrictedNumbers = [0, 1] #numbers to only appear once
unrestrictedNumbers = [num for num in categories if num not in restrictedNumbers] #can appear any number of times
matrix = [[0, 0, 0, 0, 0] for l in range(10)] #10 rows, 5 columns matrix of zeros
def returnShuffled(startList):
random.shuffle(startList)
return startList
newMatrix = [returnShuffled(restrictedNumbers + [random.choice(unrestrictedNumbers) for _ in range(len(row) - len(restrictedNumbers))]) for row in matrix]
pprint.pprint(newMatrix)