MATLAB: How to create color checkerboard any size

checkerboardclassdata type

should i make a black white checkerboard and convert it to coloured one? how to make it with random coloures?

Best Answer

I think you have the right idea with generating a black and white checkerboard first.
If you have a 3x3 array, you have 9 total squares. The pattern of odd/even is [1 0 1 0 1 0 1 0 1]. Now create an array of the two RGB colors: [0 0 0; 1 1 1]. I went with black and white but these could be any colors. Now, index that color array with your odd/even logical vector. With a final reshape and concatenation, you will have a checkerboard image.
N = 3;
colors = [0 0 0; 1 1 1];
inds = 1:N^2;
color_inds = 1+mod(inds,2);
r = colors(color_inds,1);
g = colors(color_inds,2);
b = colors(color_inds,3);
checkers = cat(2,r,g,b);
checkers = reshape(checkers,[N,N,3]);
imagesc(checkers);
axis equal tight;
Related Question