Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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

Antworten (1)

Sumeet Gadagkar
Sumeet Gadagkar am 6 Mär. 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

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by