how to create checkboard using matrix?

조회 수: 8 (최근 30일)
abid rashid
abid rashid 2017년 3월 15일
댓글: abid rashid 2017년 3월 15일
I am trying to create checkboard using matrix but not getting success. With Forloop I have developed it but having problem with matrix. How we can create checkboard pattern using matrix to get the result like in attachment.

답변 (2개)

ES
ES 2017년 3월 15일
white100_100 =repmat(1, 100, 100);%1-white color
black100_100 =repmat(0, 100, 100);%0-black color
chessboardMatrix = repmat([black100_100 white100_100;white100_100, black100_100], 4,4);
imwrite(chessboardMatrix , 'chessboard.jpg')
  댓글 수: 2
abid rashid
abid rashid 2017년 3월 15일
Thank you, how to make this result out of the above coding please. 2 black and 2 white only.
abid rashid
abid rashid 2017년 3월 15일
Got it using this
white100_100 =repmat(1, 128, 128);%1-white color
black100_100 =repmat(0, 128, 128);%0-black color
chessboardMatrix = repmat([black100_100 white100_100;white100_100, black100_100], 1,1);
imshow(chessboardMatrix );

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


Jan
Jan 2017년 3월 15일
편집: Jan 2017년 3월 15일
M = rem(bsxfun(@plus, 1:8, (1:8).'), 2);
Or since Matlab R2016b:
M = rem((1:8) + (1:8).'), 2);
[EDITED] For your 2x2 grid, this would be the best solution:
M = [1,0; 0,1];
But dynamically:
n = 2;
M = rem(bsxfun(@plus, 1:n, (1:n).'), 2);
% Or: M = rem(bsxfun(@plus, 0:n-1, (1:n).'), 2);

카테고리

Help CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by