## Matrix which is 1 when row and column are both odd or both even

I want to create a matrix which which has:

- The value 1 if the row is odd and the column is odd
- The value 1 if the row is even and the column is even
- The value 0 Otherwise.

I want to get the same results as the code below, but in a one line (command window) expression:

N=8; A = zeros(N); for row = 1:1:length(A) for column = 1:1:length(A) if(mod(row,2) == 1 && mod(column,2) == 1) A(row,column*(mod(column,2) == 1)) = 1; elseif(mod(row,2)== 0 && mod(column,2) == 0 ) A(row,column*(mod(column,2) == 0)) = 1; end end end disp(A)

This is the expected result:

1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1

A simple approach is to use implicit expansion of addition, noting that

odd+odd = even+even = 0

So this is your answer:

A = 1 - mod( (1:N) + (1:N).', 2 );

You could also do this with `toeplitz`

, as shown in this MATLAB Answers Post

For a square matrix with number of rows = number of columns = `N`

A = toeplitz(mod(1:N,2));

If the number of rows (`M`

) is not equal to the number of columns (`N`

) then

A = toeplitz(mod(1:M,2),mod(1:N,2))

FWIW, you're asking a specific case of this question:

How to generate a customized checker board matrix as fast as possible?

**m\times n$ matrix with an even number of 1s in each row and column,** Hint: If m=1 or n=1, the problem is simple. So suppose they are both >1. Fill in everything with 0's and/or 1's except the bottom row and the� Multiplying a Row by a Column We'll start by showing you how to multiply a 1 × n matrix by an n × 1 matrix. The first is just a single row, and the second is a single column. By the rule above, the product is a 1 × 1 matrix; in other words, a single number. First, let's name the entries in the row r 1 , r 2 ,

Can you take three lines?

N=8; A = zeros(N); A(1:2:end, 1:2:end) = 1; A(2:2:end, 2:2:end) = 1;

One line solution (when `N`

is even):

A = repmat([1, 0; 0 1], [N/2, N/2]);

**How to count matrices with rows and columns with an odd number of ,** For m>0, we have ∑k(−1)k(mk)=(1−1)m=0 and ∑k(mk)=(1+1)m=2m. So ∑k od d(mk)=(2m−0)/2=2m−1. Your identity is (2m−1)n−1=(2n−1)m−1. One way that some people remember that the notation for matrix dimensions is rows by columns (Rather than columns by rows) is by recalling a once popular-soda: RC Cola-- rows before columns! Below, you can see two pictures of the same matrix with the rows and columns highlighted. The dimensions of this matrix. dimensions: 2 × 3; 2 rows × 3

You can try the function `meshgrid`

to generate mesh grids and use `mod`

to determine even or odd

[x,y] = meshgrid(1:N,1:N); A = mod(x+y+1,2);

such that

>> A A = 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1

**How do I extract the odd and even rows of my matrix into two ,** Learn more about matrix, matrices, reorder, row, column, even, odd, reconstruct, for, loop, rearrange One contains the odd rows and another the even ones. Adding respectively an extra row or column to a table so that the parity of all columns or rows respectively is odd or even is always straightforward. Adding both at once adds one extra cell that is overspecified (at the intersection of extra column and row - at bottom right hand corner if we extend in length and to the right).

**This question I found in internet please help me to solve this ,** Write a function called odd_index that takes a matrix, M, as input argument and Note that both the row and the column of an element must be odd to be included in the (1,2), (2,1), (2,2) because either the row or the column or both are even. size(C,1) returns the number of rows in C, while size(C,2) returns the number of columns. size(C) returens the number of rows and columns, for matrices with higher dimentions it returns the number of vectors in each dimension

**Frequencies of even and odd numbers in a matrix,** Matrix sum except one item � Centrosymmetric Matrix � Check if Matrix sum is prime or not � Sum of middle row and column in Matrix � Program for� A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2. The constant sum in every row, column and diagonal is called the magic constant or magic sum, M

Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1. Example 1 The matrix 1 0 0 0 should be changed to following 1 1 1 0 Example 2 The matrix 0 0 0 0 0 1 should be changed to following 0 0 1 1 1 1 Example 3 The matrix 1 0 0 1 0 0 1 0

##### Comments

- Your question is a simplification of the one I've marked as a duplicate. Specifically, the accepted answer but in the case that
`m=n=N`

since your matrix is square, and`p=q=1`

since you want cheque squares which are`1*1`

. - @Wolfie, Is it considered a duplication just because the question is a subset of another question? I don't think it's a duplication for the link you gave, because the solution for the question above is much simpler than the general problem.
- @Rotem you say the solution for this question is simpler, but your two solutions are either (1) 3 lines or (2) only valid for even N... The one liner in the dupe is fairly short and sweet, and works for all cases. But you're probably right
- @MazzEbra, I meat it to be a joke... I was trying to give a quick and simple solution, and came up with 3 lines of code. I had to think a little longer to find a one line solution. I just don't think it's critical.
- @Rotem how about a little extra to yours: A = repmat([1, 0; 0 1], [N, N])(1:N,1:N);
- @Detlf That's not valid MATLAB syntax