Borrar filtros
Borrar filtros

Error: ()-indexing must appear last in an index expression. Which is the problem in line 4?? Help me pls

1 visualización (últimos 30 días)
function [A] = SecondTypeStirlingNumber(i,n) A=zeros([i n]); for j=1:n A(j)(j)=1; for k=2:i A(j)(k) = A(j-1)(k) + k*A(j)(k); end end end

Respuestas (1)

Sumeet Gadagkar
Sumeet Gadagkar el 6 de Mzo. de 2018
Hey Carlo,
While trying to index matrices in MATLAB the syntax is A(i,j) assuming 'A' is your matrix, not A(i)(j). You should also note that MATLAB indexes start from 1 and not 0, so something like A(0,0) or A(0,1) or A(2,0) will give an error which is the case for your code in line 6 in the first iteration of the for loop. Consider indexing such that a 0 index does not occur. The code is as below with the correct syntax for indexing a matrix.
function [A] = SecondTypeStirlingNumber(i,n)
A=zeros([i n]);
for j=1:n
A(j,j)=1;
for k=2:i
A(j,k) = A(j-1,k) + k*A(j,k);
end
end
end

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by