# Why do I get "Array indices must be positive integers or logical values"?

4 visualizaciones (últimos 30 días)
Mims Hillis el 6 de Mzo. de 2023
Comentada: Mims Hillis el 7 de Mzo. de 2023
I am trying to graph Thrust vs Mach number. I took posted all the parts that are related to the error message
clc
clear
close all
Ta = 504;
y = 1.4;
Ra = 1716;
for M = .1:.1:5
u(M) = M.* sqrt(y.*Ra.*Ta);
disp(u)
end
The error I get is "Array indices must be positive integers or logical values. Error in test (line 10) u(M) = M.* sqrt(y.*Ra.*Ta);" Can someone shine some light on what I am doing wrong?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Cris LaPierre el 6 de Mzo. de 2023
Editada: Cris LaPierre el 6 de Mzo. de 2023
Your values of M are not valid indices for u. Your indices must be integer values >=1.
A=1:5;
% This works because the index is a positive integer
A(2)
ans = 2
% This does not work because the index is a decimal number
A(0.2)
Array indices must be positive integers or logical values.
The most robust solution would be to create a vector M, and then have your for loop loop through each element of M, with the loop counter being used assign the result to u.
M = .1:.1:5;
for v = 1:length(M)
u(v) = M(v).* sqrt(y.*Ra.*Ta);
disp(u)
end
Even better would be to get rid of the loop altogether.
M = .1:.1:5;
u = M.* sqrt(y.*Ra.*Ta)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Mims Hillis el 7 de Mzo. de 2023
Thank you I should have mentioned that the posted code was apart of a larger loop

Iniciar sesión para comentar.

### Más respuestas (1)

Torsten el 6 de Mzo. de 2023
Ta = 504;
y = 1.4;
Ra = 1716;
M = .1:.1:5;
for i = 1:numel(M)
u(i) = M(i)*sqrt(y*Ra*Ta);
end
plot(M,u)
or simply
Ta = 504;
y = 1.4;
Ra = 1716;
M = .1:.1:5;
u = M*sqrt(y*Ra*Ta);
plot(M,u)
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

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

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by