error with indices and vectors
1 view (last 30 days)
Show older comments
Montserrat López Vargas
on 21 Sep 2020
Commented: Montserrat López Vargas
on 21 Sep 2020
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?
0 Comments
Accepted Answer
Walter Roberson
on 21 Sep 2020
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.
More Answers (1)
Image Analyst
on 21 Sep 2020
Edited: Image Analyst
on 21 Sep 2020
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
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!