How to solve "Not Enough Input Arguments" Error?

3 visualizaciones (últimos 30 días)
MATLABhelp
MATLABhelp el 13 de En. de 2019
Comentada: TADA el 15 de En. de 2019
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

Respuesta aceptada

TADA
TADA el 13 de En. de 2019
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

Más respuestas (0)

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by