フィルターのクリア

How do you create the Pascal Triangle?

25 ビュー (過去 30 日間)
Anonymous Matrix
Anonymous Matrix 2017 年 2 月 21 日
回答済み: Baltazar 2023 年 9 月 5 日
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 日
  2 件のコメント
Anonymous Matrix
Anonymous Matrix 2017 年 2 月 21 日
great! thanks!
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?

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by