How to solve "Not Enough Input Arguments" Error?

3 ビュー (過去 30 日間)
MATLABhelp
MATLABhelp 2019 年 1 月 13 日
コメント済み: TADA 2019 年 1 月 15 日
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 日
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
  2 件のコメント
MATLABhelp
MATLABhelp 2019 年 1 月 15 日
Thank you!
TADA
TADA 2019 年 1 月 15 日
Cheers

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by