How to create a checkerboard matrix without inbuilt function.

I just want to write this matrix, but want to do it using for loops
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

8 件のコメント

Riley Smith
Riley Smith 2017 年 9 月 12 日
im trying to do it using zero(16) and plussing 1's in certain coordinates, or i could use meshgrid too mesh them together, not sure how i want to do it.
Stephen23
Stephen23 2017 年 9 月 12 日
@Riley Smith: so try both of them. What is stopping you from trying things out? MATLAB does not explode if your first attempt does not work.
Adam
Adam 2017 年 9 月 12 日
This is one of those problems where there is a vast number of different ways to try it. Many that give the right answer and many that give the wrong answer, but command line testing is free. My first attempt yielded the wrong answer as I forgot that the value at the end of a column is equal to that at the start of the next column. But a little tweak soon fixes those problems and experimenting is fun and good for learning.
James Tursa
James Tursa 2017 年 9 月 12 日
If you get stuck, or just want to see how others have done this:
Rik
Rik 2017 年 9 月 12 日
Also, the criterion restricting the use of builtins makes it harde to answer this. It is very difficult to define what counts as a builtin and what doesn't, as that quickly devolves into not being able to use Matlab at all (+ is a builtin as well).
Cedric
Cedric 2017 年 9 月 12 日
編集済み: Cedric 2017 年 9 月 12 日
You can also use
abs(sin(...)) or abs(cos(...))
with the proper arguments.
James Tursa
James Tursa 2017 年 9 月 12 日
Using the sin function for this would be a sin ...
Cedric
Cedric 2017 年 11 月 28 日
編集済み: Cedric 2017 年 11 月 28 日
I had missed this comment ... ;-)
It's one of the rare cases where using a cos would be a sin too ..
0.5-(-1).^((1:n)+(1:n).')/2
or
a = 1 : n ;
a + a.' == 1

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

 採用された回答

Joseph Cheng
Joseph Cheng 2017 年 9 月 12 日

0 投票

There are much easier ways to do this than nested for loops, reshape is readily available. if you must do it with for loops you want to start off with the template
for Rind=1:Nrows
for Cind=1:Ncols
Checkerboard(Rind,Cind) = ___;%what condition of Rind and Cind gives you the checker board pattern.
%start with the first row what makes a 1 appear when Rind==1 and Cind=a number.
%then think of the sigificant difference between Rind==1 and Rind==2.....
end
end

その他の回答 (4 件)

Sangam K
Sangam K 2017 年 11 月 25 日

2 投票

%Hope this helps you.
function a = checkerboard(n)
a = zeros(n);
for i = 1:n
for j = 1:n
if (i == j)
a (i, j) = 0;
elseif (mod(j, 2) == 0) && (mod(i,2) == 0)
a(i,j) = 0;
elseif (mod(j, 2) == 0) || (mod(i,2) == 0)
a(i,j) = 1;
end
end
end
end
Jan Siegmund
Jan Siegmund 2020 年 5 月 21 日

0 投票

For an even sized checkerboard:
rows = 6;
cols = 4;
normal = repmat(eye(2,'logical'),[rows/2 cols/2]);
% or
inverted = repmat(~eye(2,'logical'),[rows/2 cols/2]);
Mendi
Mendi 2020 年 9 月 5 日

0 投票

It is more natural to use modulus on meshgrid:
[iX,iY] = meshgrid(1:8,1:8);
Mask=mod(iX+iY,2);

2 件のコメント

Bruno Luong
Bruno Luong 2020 年 9 月 5 日
One liner variant
mod((1:8)+(1:8)',2)
Mendi
Mendi 2020 年 9 月 6 日
Yeap. The one liner is better.

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

Bruno Luong
Bruno Luong 2020 年 9 月 5 日
編集済み: Bruno Luong 2020 年 9 月 5 日

0 投票

Two more methods
toeplitz(mod(0:7,2))
or for even size
kron(ones(4),[0 1;1 0])

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 9 月 12 日

コメント済み:

2020 年 9 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by