Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do matrix conversions by row and columns toggles?

Tags:

algorithm

I have got a square matrix consisting of elements either 1 or 0. An ith row toggle toggles all the ith row elements (1 becomes 0 and vice versa) and jth column toggle toggles all the jth column elements. I have got another square matrix of similar size. I want to change the initial matrix to the final matrix using the minimum number of toggles. For example

|0 0 1|
|1 1 1|
|1 0 1|

to

|1 1 1|
|1 1 0|
|1 0 0|

would require a toggle of the first row and of the last column.

What will be the correct algorithm for this?

like image 531
user160618 Avatar asked Aug 21 '09 07:08

user160618


People also ask

How do you interchange rows and columns in a matrix?

Any 2 columns (or rows) of a matrix can be exchanged. If the ith and jth rows are exchanged, it is shown by Ri ↔ Rj and if the ith and jth columns are exchanged, it is shown by Ci ↔ Cj.

How do you change a row matrix to a column matrix in Matlab?

B = A . ' returns the nonconjugate transpose of A , that is, interchanges the row and column index for each element.

How do you convert a matrix to a vector?

Conversion of a Matrix into a Row Vector. This conversion can be done using reshape() function along with the Transpose operation. This reshape() function is used to reshape the specified matrix using the given size vector.

How do you select rows in a matrix?

Similar to vectors, you can use the square brackets [ ] to select one or multiple elements from a matrix. Whereas vectors have one dimension, matrices have two dimensions. You should therefore use a comma to separate the rows you want to select from the columns.


2 Answers

In general, the problem will not have a solution. To see this, note that transforming matrix A to matrix B is equivalent to transforming the matrix A - B (computed using binary arithmetic, so that 0 - 1 = 1) to the zero matrix. Look at the matrix A - B, and apply column toggles (if necessary) so that the first row becomes all 0's or all 1's. At this point, you're done with column toggles -- if you toggle one column, you have to toggle them all to get the first row correct. If even one row is a mixture of 0's and 1's at this point, the problem cannot be solved. If each row is now all 0's or all 1's, the problem is solvable by toggling the appropriate rows to reach the zero matrix.

To get the minimum, compare the number of toggles needed when the first row is turned to 0's vs. 1's. In the OP's example, the candidates would be toggling column 3 and row 1 or toggling columns 1 and 2 and rows 2 and 3. In fact, you can simplify this by looking at the first solution and seeing if the number of toggles is smaller or larger than N -- if larger than N, than toggle the opposite rows and columns.

like image 119
Richard Dunlap Avatar answered Oct 05 '22 11:10

Richard Dunlap


It's not always possible. If you start with a 2x2 matrix with an even number of 1s you can never arrive at a final matrix with an odd number of 1s.

like image 20
Henrik Avatar answered Oct 05 '22 11:10

Henrik