How can I write a function that gives out a matrix with the specified rows and columns?

2 次查看(过去 30 天)
Write a function getfancyrectangle(R,C) that takes the number of rows and columns and returns a string as a character matrix, containing a "fancy" rectangle filled with alternating '@' and '#' characters.
For example:
R=2
C=3
@#@
#@#
  2 个评论
Jan
Jan 2014-10-27
This is obviously a homework question. As usual we will not solve it for you, such that you keep the chance to submit a solution without cheating.
Better post, what you have tried so far and ask a specific question.
S
S 2014-10-28
编辑:per isakson 2014-10-28
This is what I have so far. However, everytime I try to run the function it says there is an error and that I don't have enough input arguments. What does this mean?
function s = getfancyrectangle(R,C)
% R=4; C=5;
if mod(R,2)==0
s = getfancyrectangle(R+1,C);
s(end,:)= [];
else
s = repmat('#',R,C);
s(1:2:end) = '@';
end
end

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2014-10-27
A hint:
n = 10;
x = repmat(1, 1, n);
x(2:2:n) = 2;

per isakson
per isakson 2014-10-28
编辑:per isakson 2014-10-28
Your function as of the comment works nicely
>> s = getfancyrectangle( 4, 5 )
s =
@#@#@
#@#@#
@#@#@
#@#@#

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by