Why am I getting the error "Array indices must be positive integers or logical values."?

2.784 visualizaciones (últimos 30 días)
I want to compare the exponential of matrix A obtained by the summation formula and the one with the eigen values. However, I'm getting the following error when exponential is computed with the diagonalised form (E here).
"Array indices must be positive integers or logical values."
A = [1,2,1,3; 3,1,2,0; 2,2,1,4; 1,3,2,1];
[V,D] = eig(A);
exp_A = 0;
for k = 0:100
exp_A = exp_A + (A^k)/factorial(k);
end
disp(exp_A);
304.3054 340.7344 263.9243 320.0369 275.4796 308.2690 238.7711 289.0775 389.2552 436.7654 339.1998 410.7063 309.9581 347.7556 269.7020 326.5840
E = V*(exp(D))\V;
disp(E);
0.0008 + 0.0000i -0.0001 - 0.0000i -0.0001 + 0.0000i -0.0007 - 0.0000i -0.0001 - 0.0000i -0.8364 - 0.1567i 0.1039 + 0.0000i 0.8393 + 0.1795i -0.0001 + 0.0000i 0.1039 + 0.0000i -0.8364 + 0.1567i 0.8393 - 0.1795i -0.0007 - 0.0000i 0.8393 + 0.1795i 0.8393 - 0.1795i -0.7766 - 0.0000i
  5 comentarios

Iniciar sesión para comentar.

Respuesta aceptada

Kelly Kearney
Kelly Kearney el 5 de Dic. de 2018
My guess is you have a variable named eig sitting in your workspace. So when you try to call the eig function, the variable is overshadowing it and matlab is trying to extract the 1st, 2nd, 1st, 3rd, etc. elements from the eig array. The attempt to extract the 0th element leads to the error.
Always be careful with your choice of variable names!

Más respuestas (8)

henry espinoza
henry espinoza el 19 de Jun. de 2020
Array indices must be positive integers or logical values.
Error in Tarea3_HE (line 15)
Xc1 = (-1 /(PRM(u)*(w)))*j

Roya Kakar
Roya Kakar el 19 de En. de 2022
Please answer my quesion. I get the following error after running this line of code
sm =sum(table2array(X));
Array indices must be positive integers or logical values.

laura celis
laura celis el 12 de Mzo. de 2022
tengo este error
Array indices must be positive integers or logical values.
Error in recortedesenales (line 38)
z1= z(A:B);

Roshan Siwakoti
Roshan Siwakoti el 21 de Oct. de 2022
a(i) =((delta_p2)/(2*tau(j)*(t_traj(j+1)-t_traj(j))))-((delta_p1)/(2*tau(j)*(t_traj(j)-t_traj(j-1))))
Array indices must be positive integers or logical values
  3 comentarios
Kelly Kearney
Kelly Kearney el 21 de Oct. de 2022
Possible guess... Is a(i) supposed to be a(j)? If you haven't otherwise set i to be an integer, it defaults to sqrt(-1), which cannot be used as an index. Same goes for j; check that it's an integer. Best practices in Matlab recommend avoiding i or j as indices for this very reason.
Walter Roberson
Walter Roberson el 21 de Oct. de 2022
I predict that your j is 1 so j-1 is 0 which is not a valid index.

Iniciar sesión para comentar.


Ferlyn
Ferlyn el 14 de Nov. de 2022
% c. x(t) = 3*exp(-2*t)u(t)
subplot (4,2,3)
y = 3*exp(-2*t).*(u(t));
plot (t,y,'-g','Linewidth', 2);
axis ([-1 5 -1 5]);
grid on;
xlabel ('t in sec');
ylabel ('x(t)');
title ('plot of (c)');
Here's my code and it says "Array indices must be positive integers or logical values". What should I do to fix it? Thank you.

Nerea Espinosa Giralt
Nerea Espinosa Giralt el 19 de Nov. de 2022
Hola!
Tengo este error." Array indices must be positive integers or logical values."
Atenuacion=(k*R_001^alfa*u(p))*(R_001)^(0.38/Ls-0.25/(1+n(p/0.01)^(-0.36)*longitud^m)*Ls);
¿podriais ayudarme por favor?
Gracias,
Saludos
  3 comentarios
Steven Lord
Steven Lord el 19 de Nov. de 2022
The section of code with n(p/0.01) also looks like it might be missing a multiplication operator. If it isn't and you intended p/0.01 to give you an integer value you can use as an index into n, you may encounter difficulties due to floating point arithmetic. Even if that expression did happen to give an integer value (or you called round to force it to give an integer value) as written unless that integer value is 1 you're asking for an element of n that doesn't exist.
x = 1;
y = x(2) % There is no second element
Index exceeds the number of array elements. Index must not exceed 1.

Iniciar sesión para comentar.


Fariha
Fariha el 12 de Ag. de 2023
Hello, my code:
yoy(j,1)=100*(log(Real_Price(ii,1))-log(Real_Price(ii-4,1)));
every time I run it says 'Array indices must be positive integers or logical values.'
can you help me understand my problem?
  1 comentario
DGM
DGM el 12 de Ag. de 2023
MATLAB sees that you're trying to index into an array. Array indices must be real positive (nonzero) integers (or logicals). So:
Is the index jj a real, positive integer?
Is the index ii a real, positive integer?
Is ii>4 so that ii-4 is always positive?
Is log() still interpreted as a function, or have you created a variable with the same name?

Iniciar sesión para comentar.


Maximilian
Maximilian el 18 de Ag. de 2023
Hello, why am I getting the "Array indices must be positive integers or logical values." error for the code below. I cleared the workspace so that shouldn't be a problem.
Vi = 0;
Vf = 1;
R = 1000;
C = 10 * 10^-6;
t = linspace(0,0.1,1001);
r = R*C;
V(t) = Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
Plot(t,V(t))
  2 comentarios
Stephen23
Stephen23 el 19 de Ag. de 2023
Editada: Stephen23 el 19 de Ag. de 2023
You Index into V using t:
V(t) = ...
Are the values of t valid indices? (hint: no) Get rid of them:
Vi = 0;
Vf = 1;
R = 1000;
C = 10 * 10^-6;
t = linspace(0,0.1,1001);
r = R*C;
V = Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
plot(t,V)
Walter Roberson
Walter Roberson el 19 de Ag. de 2023
It is common to get confused between a formula and an array
A line such as
V(t) = Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
is typically intended to be a formula -- the left and right t are intended to refer to values
MATLAB does support creating formulas with that syntax, but only using the Symbolic Toolbox, such as
syms t
V(t) = Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
MATLAB would then refer to V as a "symbolic function".
MATLAB main support for creating formulas is for numeric formulas (that do not typically involve the Symbolic Toolbox). Those use a different syntax called "anonymous functions" that would look like
V = @(t) Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
In both the case of the "symbolic function" and this "anonymous function", V would become an object describing calculations that would be done if you pass values to the object, such as
V(3.2)
In both the symbolic function and the anonymous function, the t would refer to some indefinite value of t to be given value later, and the symbolic or anonymous function describes how to calculate the output in that future time when you finally get passed a specific value in place of t
But if you are not working with symbolic functions or anonymous functions (or some Object Oriented classes) then () is either for passing parameters to a function, or else for indexing. In the original code
t = linspace(0,0.1,1001);
V(t) = Vi + (Vf-Vi).*(1 - (2.72 .^(-t/r)));
you are working with definite numeric values for t not with a symbolic variable, so the right hand side is calculating definite numeric values, and the left side V(t) would be considered indexing.
Almost any time you have definite numeric values for t, V(t) on the left side of an assignment would be interpreted as an attempt to index. (There are some potential exceptions for Object Oriented classes.)

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by