How do you create the Pascal Triangle?
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (2)
Hans Jakob Rivertz
il 27 Gen 2019
2 voti
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
il 5 Set 2023
0 voti
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
1 Commento
Burkhard Blaich
il 10 Ott 2024
toll
Categorie
Scopri di più su Interpolation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!