The Pascal\'s triangle can be displayed as elements in a lower-triangular matrix
ID: 3591981 • Letter: T
Question
The Pascal's triangle can be displayed as elements in a lower-triangular matrix as shown below for 6 rows of Pascal's triangular matrix. Write a MATLAB program that creates a n×n matrix that displays n rows of Pascal's triangle. The program must prompt the user to enter the number n of rows in the lower triangular matrix and then create the matrix. (One way to calculate the value of the elements in the lower portion of the matrix is 1 0 0 0 00 1 2 1 00 0 1 4 6 4 1 0 Cij = (j-1)!(i-j)! 15 10 10 5 1 Use the program to create 4 and 7 rows Pascal's triangles. You can use the built-in function factorial ) in MATLAB. Only the Pascal's triangular matrix (PT) must be displayed in the Command Window. Do Not Use disp or fprintf in your program.Explanation / Answer
Hi,
Pascal's triangle is given by, from third row, every element is the sum of the enumber from the previous row before our column and the number from the previous row in our column.
we can simulate this in matlab as follows,
function p=pascal(n)% p is the matrix to be returned
p(1, 1) = 1;
p(2, 1 : 2) = [1 1]; %since both rows are constant
for r = 3 : n %calculating from 3rd row
pt(r, 1) = 1; %1st element is 1 in every row
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for column = 2 : row-1
p(row, column) = p(row-1, column-1) + p(row-1, column);%adding values as defined above and assigning
end
pt(row, row) = 1;
end
P= pascal(7)%calling function to return pascal triangle with 7 rows
P= pascal(4)%with 4 rows
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.