Matrix with Bell Triangle

조회 수: 2 (최근 30일)
Millone
Millone 2015년 5월 22일
댓글: Millone 2015년 5월 22일
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
Millone
Millone 2015년 5월 22일
No, I have no tried it but I give it a test. Thanks
Millone
Millone 2015년 5월 22일
I do not get it! I am just approaching Matlab!

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

채택된 답변

John D'Errico
John D'Errico 2015년 5월 22일
Simple. Just use loops.
n = 5;
BellTri = zeros(n);
BellTri(1,1) = 1;
for i = 2:n
BellTri(i,1) = BellTri(i-1,i-1);
for j = 2:i
BellTri(i,j) = BellTri(i - 1,j-1) + BellTri(i,j-1);
end
end
BellTri
BellTri =
1 0 0 0 0
1 2 0 0 0
2 3 5 0 0
5 7 10 15 0
15 20 27 37 52

추가 답변 (3개)

Titus Edelhofer
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
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
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

카테고리

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