Index exceeds number of array elements
1 次查看(过去 30 天)
显示 更早的评论
when i run my code, it says index exceeds number of array elements. But i have used sum() which should by itself determine the array length, right? The final value of expression is a single integer which varies continuously in each iteration of loop.
0 个评论
采纳的回答
Ameer Hamza
2020-9-30
编辑:Ameer Hamza
2020-9-30
No, the error is caused by missing multiplication operators on line 33. * Should be placed after Kad at two places on this line.
A new error occurs after the above correction that is caused because you are trying to access the 299th element of vector Cs, whereas it only has 294 elements.
3 个评论
更多回答(1 个)
Walter Roberson
2020-9-30
You have
Ef = (((Kad -(Aem*Kad(1+(((i-1)*sum(Cs,2))/K1s)))-(((i-1)*(sum(Cl,2)-Cl(1)-Cl(2)))/K1l)- (Cl(1)/KG1)- (Cl(2)/KG2)-1)) + ((((Kad -(Aem*Kad(1+(((i-1)*sum(Cs,2))/K1s)))-((i-1)*((sum(Cl,2)-Cl(1)-Cl(2))/K1l))- (Cl(1)/KG1)- (Cl(2)/KG2)-1))^2) + 4*Kad*((((i-1)*sum(Cl,2)-Cl(1)-Cl(2))/K1l)+(Cl(1)/KG1)+(Cl(2)/KG2)+1))^0.5)/(2*Kad*((((i-1)*sum(Cl,2)-Cl(1)-Cl(2))/K1l)+(Cl(1)/KG1)+(Cl(2)/KG2)+1));
That is a request to index the scalar Kad at offset (1+(((i-1)*sum(Cs,2))/K1s)) . That offset happens to be an integer that is greater than 1.
Reminder; MATLAB has absolutely no implied multiplication. When you see something of the form A(B) then it always means either invoking function A with parameter B, or else indexing array A with index B. It **never* means multiplying A by B. Not anywhere in MATLAB. Not even inside the internal programming language of the symbolic engine.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!