Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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

Réponses (1)

Sumeet Gadagkar
Sumeet Gadagkar le 6 Mar 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

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by