How to make a checkerboard function

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 件のコメント

James Tursa
James Tursa 2020 年 5 月 11 日
編集済み: James Tursa 2020 年 5 月 11 日
Do you have any function code written yet? Do you have any ideas on how you might fill in the element of M inside this function?
Hint: zeros(n) will give you an nxn matrix filled with 0's. Then you can write some code to fill in the 1's in the appropriate spots.
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 日

0 投票

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...

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

2020 年 5 月 11 日

コメント済み:

2020 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by