error with indices and vectors

1 次查看(过去 30 天)
i= 7.5;
A= 300000;
P= 8000;
n=[6:6:60]';
B= A(1+i/12).^n-(P/(i/12)).*((1+i/12).^n-1);
table(n, B)
Array indices must be positive integers or logical values.
can someone help me with this error please?

采纳的回答

Walter Roberson
Walter Roberson 2020-9-21
MATLAB has no implied multiplication.
A(1+i/12) is syntactically always either a request to execute a function indicated by A with parameter 1+i/12, or else a request to index array A at location 1+i/12 .
As we can see a definition for the variable A, we know that in this case it must be a request to index A at 1+i/12 . But i is 7.5 and 1+i/12 will definitely not be a positive integer, so that is not valid indexing.
If you want to multiply A and (1+i/12) then you need to explicitly multiply, using either the .* or * operator.

更多回答(1 个)

Image Analyst
Image Analyst 2020-9-21
编辑:Image Analyst 2020-9-21
A is not a vector so there is no (1 + 1/12)th element of it. A is a scalar. Maybe you meant A * (1 + i/12)???
i= 7.5;
A= 300000;
P= 8000;
n=[6:6:60]'
B= A * (1+i/12).^n-(P/(i/12)).*((1+i/12).^n-1)
t = table(n, B)
t =
10×2 table
n B
__ __________
6 5.301e+06
12 9.7383e+07
18 1.7929e+09
24 3.3012e+10
30 6.0783e+11
36 1.1192e+13
42 2.0608e+14
48 3.7944e+15
54 6.9866e+16
60 1.2864e+18

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by