Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Answers (1)

Sumeet Gadagkar
Sumeet Gadagkar on 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

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!