Matrix with Bell Triangle
古いコメントを表示
I need to reshape a portion of a matrix computed with the values of a bell triangle where n is the number of rows of a matrix. How can I generate a matrix contain the bell values? I mean, as an example, for n= 4 but could be any value:
1
1 2
2 3 5
5 7 10 15
empty space with 0.
6 件のコメント
Guillaume
2015 年 5 月 22 日
How do you represent that triangle as a matrix (which by definition is a rectangle) ?
Millone
2015 年 5 月 22 日
Titus Edelhofer
2015 年 5 月 22 日
Hi Millone,
a loop over the rows and using cumsum will do the job ... have you tried this?
Titus
Millone
2015 年 5 月 22 日
Millone
2015 年 5 月 22 日
採用された回答
その他の回答 (3 件)
Titus Edelhofer
2015 年 5 月 22 日
O.k., when there is a full solution, I'm happy to share mine as well ;-)
function B = bell(n)
B = zeros(n);
B(1,1) = 1;
for row=2:n
B(row, 1:row) = B(row-1,row-1) + cumsum([0 B(row-1, 1:row-1)]);
end
Titus
Andrei Bobrov
2015 年 5 月 22 日
n = 5;
a = zeros(n);
a(1) = 1;
for jj = 1:n-1
a(jj+1,1:jj+1) = cumsum(a(jj,[jj,1:jj]),2);
end
Guillaume
2015 年 5 月 22 日
Assuming you're talking about this, I've tried to come up with a clever way to generate it, but didn't find any (didn't spend too much time on it either). When all else fail, loops always work:
bell = zeros(n);
bell(1) = 1;
for row = 2 : n
bell(row, 1) = bell(row-1, row-1);
for col = 2 : row
bell(row, col) = bell(row, col-1) + bell(row-1, col-1);
end
end
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!