JOHN
JOHN

Reputation: 1511

How to generate 0 and 1 matrix based on conditions in Python

I am doing a scheduling task. I translate the problem to generate an 8*8 matrix with 0 and 1 in it such that each row sums to 1 and each column sums to as close to 1 as possible.

The columns are actually a day divided into 8 pieces. Rows are the days. And 0 and 1 represent two tasks.

Upvotes: 0

Views: 200

Answers (1)

webmite
webmite

Reputation: 575

Take an identity matrix and then simply re-order the rows.

enter image description here

If your matrix is Not NxN then you apply same rules to the largest NxN portion of it and then continue by wrapping the identity function from top-to-bottom, and right-to-left. You are free to swap rows within the NxN portion to redistribute the active hours of the task. In this example columns G and H are outliers...you are free to swap the cells in G and H rows 1 and 2 with any of the other G and H cells rows 3,4,5 and 6.

enter image description here

In this example rows 7 and 8 are the outliers... you are free to swap cells in 7 and 8 columns A and B with any of the other cells in rows 7 and 8 columns C,D,E and F.

enter image description here

Upvotes: 1

Related Questions