Write a function called bell that returns the first n rows of the Bell triangle, where n is an input argument. The function must return an n-by-n array where the the top left triangle contains the Bell triangle with each row of the Bell triangle posi

2 次查看(过去 30 天)
"Write a function called bell that returns the first n rows of the Bell triangle, where n is an input argument. For a precise definition, see http://en.wikipedia.org/wiki/Bell_triangle. The function must return an n-by-n array where the top left triangle contains the Bell triangle with each row of the Bell triangle positioned diagonally—bottom-left-to-upper-right—and the bottom right triangle contains only zeros. If n is not a positive integer, the function returns an empty array.
program
function B = bell(n)
B(1,1) = 1;
for i=2:n
B(i,1) = B(1,end);
for j = 1:i-1
B(i-j,j+1) = B(i-j+1,j)+B(i-j,j);
end
end
end
error
Your function made an error for argument(s) -1
can any one help me advance wishes

采纳的回答

Walter Roberson
Walter Roberson 2015-6-15
Your code is not paying attention to the requirement,
If n is not a positive integer, the function returns an empty array.

更多回答(1 个)

charu sharma
charu sharma 2015-8-27
You should add a condition to check if n is a positive integer or not. Here is a complete solution of this program: http://farzicoders.blogspot.in/2015/08/write-function-called-bell-that-returns.html

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by