How to make a checkerboard function

2 vues (au cours des 30 derniers jours)
Mojisola Ajayi
Mojisola Ajayi le 11 Mai 2020
Commenté : Walter Roberson le 14 Mai 2020
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 commentaires
Mojisola Ajayi
Mojisola Ajayi le 11 Mai 2020
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 le 11 Mai 2020
You could write two nested for-loops over the elements of M and fill in the 1's inside those loops.

Connectez-vous pour commenter.

Réponses (1)

Guru Mohanty
Guru Mohanty le 14 Mai 2020
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 commentaire
Walter Roberson
Walter Roberson le 14 Mai 2020
Note that this is a homework question...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by