Given an integer n, make an n-by-n matrix a made up of alternating ones and zero
ID: 2079612 • Letter: G
Question
Given an integer n, make an n-by-n matrix a made up of alternating ones and zeros as shown below. The a(1,1) should be 1.
Input = n = 5
Make a Checkerboard Matrix Recent best solution Given an integer n, make an n-by-n matrix a made up of alternating ones and zeros as shown below. The a(1,1) should be 1. nput n 5 output a [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 Instructions: To solve this problem, modify the template bellow with your code. Leave the name of the function and variables unchanged. Otherwise your code will not pass the test. Click Test to test your solution before submitting it. When you are satisfied with it click submitExplanation / Answer
Ans)
Matlab Code
-----------------
function a = checkerboard(n)
%CHECKERBOARD Summary of this function goes here
% Detailed explanation goes here
% case of even rows
if mod(n, 2) == 0
a = ones(n + 1, n); % create a matrix of ones having a plus row
a(2 : 2 : end) = 0; % select every second element
a(end, :) = []; % remove the last row from the matrix
else
a = ones(n, n); % create the matrix of ones
a(2 : 2 : end) = 0; % select every second element
end
end
-----------------------------------
Run in command window as below
>> checkerboard(5)
ans =
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.