How do you create the Pascal Triangle?

조회 수: 112 (최근 30일)
Anonymous Matrix
Anonymous Matrix 2017년 2월 21일
댓글: John D'Errico 2024년 10월 10일
this is for my own curiosity. how do you create the Pascal triangle in MATLAB without using the pascal() function? I assume that you're going to need a grid of zeros and a FOR loop to fill in the matrix. But how do you actually build it?

채택된 답변

KSSV
KSSV 2017년 2월 21일
  댓글 수: 3
Carter ONeal
Carter ONeal 2017년 10월 26일
I have been playing with this code and cannot figure out how you can create the full triangle. How would you make both sides of the triangle appear, not just the right hand half?
John D'Errico
John D'Errico 2024년 10월 10일
It DOES create the full triangle! Look carefully at what you see. Is there a difference between
1
1 1
1 2 1
1 3 3 1
and this?
1
1 1
1 2 1
1 3 3 1
Only that one of them is left justified and the pother is not.

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

추가 답변 (2개)

Hans Jakob Rivertz
Hans Jakob Rivertz 2019년 1월 27일
This function will do it.
function [ A ] = getpascal( n )
% Calculates the pascal triangle.
% Will give exact values for n<56
if(n>1030); error('Argument should be less than 1031'); end
A=eye(n);
A(:,1)=1;
for i=2:n
A(i,2:end)=A(i-1,1:end-1)+A(i-1,2:end);
end
end

Baltazar
Baltazar 2023년 9월 5일
function y = MypascaTriangle(n)
% calculate the MypascaTraingle
% Will give exact values
y=1;
disp(y)
for i=1:n
y=conv(y,[1 1]);
disp(y)
end

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by