필터 지우기
필터 지우기

Matrix Solution

조회 수: 14 (최근 30일)
Jair
Jair 2011년 11월 21일
댓글: Joshua Magno 2016년 10월 13일
The elements of the symmetric Pascal matrix are obtained from: Pij = (i + j - 2)!/(j - 1)!(j - 1)! Write a MATLAB program that creates an n by n symmetric Pascal matrix. Use the program to create a 4x4 and 7x7 Pascal matrices.
  댓글 수: 1
Joshua Magno
Joshua Magno 2016년 10월 13일
function X=matrix
n=input('Enter the number of rows: ');
m=input('Enter the number of columns: ');
A=[]; % define an empty matrix
for k=1:n
for h=1:m if k==1
A(k,h)=k;
elseif h==1
A(k,h)=h;
else
A(k,h)=A(k,h-1) + A(k-1,h); %assign values to other elements
end
end
end
A

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

답변 (2개)

Amith Kamath
Amith Kamath 2011년 11월 21일
for i = 1:7
for j = 1:7
P(i,j) = factorial(i + j - 2)./(2*factorial(j - 1));
end
end
for a 4x4 matrix, change the 7 in lines 1 and 2 to 4! But with this formulation, I wonder how it can be symmetric. I'm guessing you've mistyped the question and the denominator has to be (i-1)!(j-1)! which will set things correct!
and hence the line 3 in the code above will be:
P(i,j) = factorial(i + j - 2)./(factorial(j - 1)*factorial(i - 1));
Oh, and BTW, MATLAB (as usual), has a function called pascal, and you need to just say pascal(4) or pascal(7)!

Andrei Bobrov
Andrei Bobrov 2011년 11월 21일
n = 7;
k = factorial(0:n-1);
out = factorial(bsxfun(@plus,1:n,(1:n)')-2)./bsxfun(@times,k,k');

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by