error with indices and vectors

1 visualización (últimos 30 días)
Montserrat López Vargas
Montserrat López Vargas el 21 de Sept. de 2020
Comentada: Montserrat López Vargas el 21 de Sept. de 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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Sept. de 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.

Más respuestas (1)

Image Analyst
Image Analyst el 21 de Sept. de 2020
Editada: Image Analyst el 21 de Sept. de 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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by