How to write a Function that returns a matrix

Write a function named checkerBoard that will receive a positive integer n as an input argument. The function will return an n-by-n matrix made up of alternating ones and zeros as shown in the example below. The first element of the matrix returned (first row, first column: ans(1,1) ) should be 1. I'm stuck on how to write the for loop?

댓글 수: 4

Jan
Jan 2016년 9월 30일
There is no "example below". It sounds like you've copied the text of your homework assignment without showing any own effort to solve the problem. Please post, what you have tried so far.
Thorsten
Thorsten 2016년 9월 30일
Sarah, this is obviously homework. So what have you tried so far? Any ideas of how to generate the checkerboard?
Sarah Sadeq
Sarah Sadeq 2016년 10월 3일
편집: Walter Roberson 2016년 10월 3일
I tried this right her
function [ mat ] = checkboard(n)
mat=zeros(n,n);
if rem(n+1,2)==0
mat(1:2:end,2)=1;
mat(2:2:end,2)=1;
else
mod(n,n);
mat(1:2:end)=1;
end
end
However, it doesn't work
mod(n,n); doesn't do anything useful. It calculates a value and then throws the value away.

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

답변 (1개)

Chi-Hsien Tang
Chi-Hsien Tang 2016년 9월 30일
편집: Chi-Hsien Tang 2016년 9월 30일

0 개 추천

Maybe try checkerboard(1,2,2)==0?

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

질문:

2016년 9월 30일

댓글:

2016년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by