Function Output Not Displaying Why??

function PT=myPascal(n)
PT=zeros(n,n);
for i=1:n
for j=1:i
if n>0
PT(i,j)=factorial(i-1)/(factorial(j-1)*factorial(i-j));
else fprintf ('Error: The input argument is not a positive integer >0')
end
end
end
>> myPascal(5)
ans =
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
>> myPascal(-1)
ans =
[]

回答(1 个)

You have
for i=1:n
for j=1:i
if n>0
The "for i" loop will not have its body executed unless 1:n is non-empty, which requires that n be at least 1. Therefor the test "if n>0" will never be reached unless n is at least 1, so "if n>0" can never be true in that code.

2 个评论

im confused in which part of the code i have defined something = 0?
Correction: n>0 can never be false for that code.
Your code has PT=zeros(n,n) which sets PT to zero.
You should correct your code to
if n < 1
fprintf ('Error: The input argument is not a positive integer')
else
for i=1:n
for j=1:i
PT(i,j)=factorial(i-1)/(factorial(j-1)*factorial(i-j));
end
end
end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by