How to solve "Not Enough Input Arguments" Error?

I am trying to Write a function capable of displaying Pascal’s triangle for any number of rows to be specified as input.
My code is:
function pt = pascal_triangle(n)
% The first two rows are constant
pt(1, 1) = 1;
pt(2, 1 : 2) = [1 1];
% If only two rows are requested, then exit
if n < 3
return
end
for r = 3 : n
% The first element of every row is always 1
pt(r, 1) = 1;
% Every element is the addition of the two elements
% on top of it. That means the previous row.
for c = 2 : r-1
pt(r, c) = pt(r-1, c-1) + pt(r-1, c);
end
% The last element of every row is always 1
pt(r, r) = 1;
end
However, I get the following error:
Not enough input arguments.
Error in Draft1 (line 8)
if n < 3

 채택된 답변

TADA
TADA 2019년 1월 13일

1 개 추천

you probably just ran the function using F5, didn't you?
the function works, you just need to invoke it properly:
A = pascal_triangle(5);
you can run that from command window or a script for testing purposes, or by adding parameters to the F5 run command.
you can also add custom validation or default values using nargin (throw one of these at the beginning of your function):
if nargin < 1
n = 2; % set n to be 2 by default if not specified
end
% or alternatively validate the number of input arguments and throw a custom exception
if nargin < 1
error('throw your custom error here with a custom error message if you like');
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Argument Definitions에 대해 자세히 알아보기

질문:

2019년 1월 13일

댓글:

2019년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by