Matrix Solution
35 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Joshua Magno
am 13 Okt. 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
Antworten (2)
Amith Kamath
am 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)!
0 Kommentare
Andrei Bobrov
am 21 Nov. 2011
n = 7;
k = factorial(0:n-1);
out = factorial(bsxfun(@plus,1:n,(1:n)')-2)./bsxfun(@times,k,k');
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!