MATLAB CODY CHALLENGE Problem 4 | Checkerboard matrix


Given an integer n, make an n-by-n matrix made up of alternating ones and zeros as shown below. The a(1,1) should be 1.
Input  n = 5
 Output a is    [1 0 1 0 1
                       0 1 0 1 0
                       1 0 1 0 1
                       0 1 0 1 0
                       1 0 1 0 1]

CODE:



clc
m=input('Enter the number:');
x=[1 0];
Y=[];
for i=1:(m*m)
    Y=[Y x];
end
k=1;
Z=[];
for i=1:m
    for j=1:m
        Z(i,j)=Y(k);
        k=k+1;
    end
end

Explanation:

Output:

Reference Video:


No comments