How to make a checkerboard function

조회 수: 9 (최근 30일)
Mojisola Ajayi
Mojisola Ajayi 2020년 5월 11일
댓글: Walter Roberson 2020년 5월 14일
I'm supposed to write a function with header [M] = myCheckerBoard(n) and M is an n by n matrix, how do I do this?
M = [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]
  댓글 수: 3
Mojisola Ajayi
Mojisola Ajayi 2020년 5월 11일
I had a function written but it didn't work. I did the first step with zeros, but I'm not sure on where to proceed from there
James Tursa
James Tursa 2020년 5월 11일
You could write two nested for-loops over the elements of M and fill in the 1's inside those loops.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guru Mohanty
Guru Mohanty 2020년 5월 14일
Hi, I understand you are trying to make a checkerboard function. You can do this by two nested for loops. Here is a sample code for it.
function M = myCheckerBoard(n)
M = zeros(n,n);
for j = 1:n
if mod(j,2)==1
for i =1:2:n
M(j,i) = 1;
end
elseif mod(j,2)==0
for t = 2:2:n
M(j,t) = 1;
end
end
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 5월 14일
Note that this is a homework question...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by