Effacer les filtres
Effacer les filtres

Matrix Solution

35 vues (au cours des 30 derniers jours)
Jair
Jair le 21 Nov 2011
Commenté : Joshua Magno le 13 Oct 2016
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 commentaire
Joshua Magno
Joshua Magno le 13 Oct 2016
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

Connectez-vous pour commenter.

Réponses (2)

Amith Kamath
Amith Kamath le 21 Nov 2011
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 le 21 Nov 2011
n = 7;
k = factorial(0:n-1);
out = factorial(bsxfun(@plus,1:n,(1:n)')-2)./bsxfun(@times,k,k');

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by