How can I fill a matrix in certain places with intervals?

1 view (last 30 days)
Hi.
I need to make a matrix that resembles a chessboard, however the number of pixels and how many squares are going to be decided by the user. So far I've managed to do just that:
N = input('How many squares ')
L = input('How many pixels ')
tab = ones(L*N,L*N);
k = 1;
for i=1:N^2
if mod(i,2) ~= 0
tab(i:L,i:L) = 0;
end
I can only "draw" the first square. How can I do the others? I know it's a silly question but I'm new in MATLAB.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 29 Feb 2020
Edited: KALYAN ACHARJYA on 29 Feb 2020
N=input('How many squares: ');
tab=ones(N,N);
tab(1:2:N,2:2:N)=0;
tab(2:2:N,1:2:N)=0;
imshow(logical(tab));
  1 Comment
Guilherme Araujo
Guilherme Araujo on 29 Feb 2020
Thank you! Can you explain tab(1:2:N,2:2:N)=0; and tab(2:2:N,1:2:N)=0;? Also, I need one more input, which is how many pixels each square has. How can I do that?

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!