How to write a Function that returns a matrix

6 ビュー (過去 30 日間)
Sarah Sadeq
Sarah Sadeq 2016 年 9 月 30 日
コメント済み: Walter Roberson 2016 年 10 月 3 日
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 件のコメント
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
Walter Roberson
Walter Roberson 2016 年 10 月 3 日
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 日
Maybe try checkerboard(1,2,2)==0?
  1 件のコメント
Sarah Sadeq
Sarah Sadeq 2016 年 9 月 30 日
should I use the if statement?

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by