Matrix Solution

36 次查看(过去 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