matlab - How to generate a customized checker board matrix as fast as possible? -
i need function creates checker board matrix m
rows , n
columns of p*q
rectangles. modified third solution here that:
function [i] = mycheckerboard(m, n, p, q) nr = m*p; nc = n*q; = floor(mod((0:(nc-1))/q, 2)); j = floor(mod((0:(nr-1))/p, 2))'; r = repmat(i, [nr 1]); c = repmat(j, [1 nc]); = xor(r, c);
it works no problem:
i=mycheckerboard(2, 3, 4, 3) = 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1
but it's not fast enough since there lots of calls of function in single run. there faster way result? how can remove floating point divisions and/or calls of floor
function?
i suggest first creating binary matrix checkerboard's fields, using built-in kron
blow necessary size:
m = 2; n = 3; p = 4; q = 3; [im,in] = meshgrid(1:m,1:n); = zeros(m,n); a(mod(im.'+in.',2)==1) = 1; board = kron(a,ones(p,q))
Comments
Post a Comment