Matrix Solution

29 ビュー (過去 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');

Community Treasure Hunt

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

Start Hunting!

Translated by