write a matrix board from a scalar n

6 views (last 30 days)
dickson jane
dickson jane on 3 May 2015
Edited: Andrew Crawford on 15 Nov 2015
how to write a function called checkerboard that takes as input two positive integer scalars, n and m, in that order. The function must create and return board, which is an n-by-m matrix. Every element of board is either 0 or 1. The first element, board(1,1) is 1. No direct neighbors in the matrix, vertically or horizontally, can be equal. That is, a 1 element cannot have 1 immediately preceding or following it in the same row or column.

Answers (3)

John D'Errico
John D'Errico on 3 May 2015
Edited: John D'Errico on 3 May 2015
n = 2;
m = 3;
Solution number 1:
c = rem(bsxfun(@plus,(1:n)',0:(m-1)),2)
c =
1 0 1
0 1 0
Or this one (#2):
[r,c] = meshgrid(1:m,1:n);
c = rem(1+r+c,2)
c =
1 0 1
0 1 0
Or this one (#3):
c = toeplitz(rem(1:n,2),rem(1:m,2))
c =
1 0 1
0 1 0
Or this one (#4):
c = zeros(n,m);
c(1:2:n,1) = 1;
for ind = 2:m
c(:,ind) = ~c(:,ind-1);
end
c
c =
1 0 1
0 1 0
Or this one (#5):
c = zeros(n,m);
c(1:2:n,1) = 1;
for ind = 2:m
c(:,ind) = 1-c(:,ind-1);
end
c
c =
1 0 1
0 1 0
Or this one (#6):
c = ones(n,m);
c(1,:) = 1:m;
c = rem(cumsum(c,1),2)
c =
1 0 1
0 1 0
Or this one (#7):
c = hilb(max(n,m));
c = rem(round(1./c(1:n,1:m)),2)
c =
1 0 1
0 1 0
Or this one (#8):
c = max(0,kron(2*rem(1:n,2)'-1,2*rem(1:m,2)-1))
c =
1 0 1
0 1 0
Or this one (#9):
c = max(0,(2*rem(1:n,2)'-1)*(2*rem(1:m,2)-1))
c =
1 0 1
0 1 0
All of the above solutions should work for odd or even n and m. I'll concede that only the loop based solutions (#4 or #5, depending on whether you will accept the not operator) will be acceptable for most homework assignments. Solution 5 is quite innocuous, using nothing more than a basic loop and a call to the zeros function to initialize the array. In fact, if you don't like the use of zeros, you can drop that statement completely, since it only speeds up the code by preallocating c.
Running out of ideas here, but there are surely some others. I'll offer one last solution, (#10) that will work only some of the time, and not terribly predictably so.
c = round(rand(n,m));
The point is that there are MANY ways to solve any problem in a language like MATLAB. Not all ways are always as good as others, depending on speed and memory issues at times. Pick a way that makes sense to you. Learn from the rest.

Andrew Crawford
Andrew Crawford on 15 Nov 2015
Edited: Andrew Crawford on 15 Nov 2015
I attained a solution using the diag function along with the repeat and a bit of tweaking.
function board = checkerboard(n,m)
a = diag(diag(ones(2)));
b = repmat(a,n,m);
board = b(1:n,1:m);
end

Image Analyst
Image Analyst on 3 May 2015
I hope this isn't your homework -- you didn't tag it as such. Here's is one solution:
function test
clc;
n = 5
m = 7
c = MakeCheckerboard(n, m)
function c = MakeCheckerboard(n, m)
c = zeros(n, m);
c(1:2:end) = 1;
Of course there is also the checkerboard() function in the Image Processing Toolbox.
  4 Comments
John D'Errico
John D'Errico on 3 May 2015
As I pointed out, Image Analysts's solution will not work for even n.
n = 2;
m = 3;
c = rem(bsxfun(@plus,(1:n)',0:(m-1)),2)
c =
1 0 1
0 1 0
dickson jane
dickson jane on 3 May 2015
yes it works for all the cases john thanks

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!