Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 submit

Explanation / 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote