Effacer les filtres
Effacer les filtres

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

44 vues (au cours des 30 derniers jours)
I keep getting the "Array indices must be positive integers or logical values" error for the line of my code "fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));" that comes after the "else". How do I fix this? My entire entry of code is below.
C0 = 8;
k = 0.13;
FS = linspace(0,0.4079,100);
CS_scheil = k*C0*(1 - FS).^(k - 1);
for i = 0:0.01:1
if i == 1
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
a(i) = 1;
b(i) = (k*C0*((1 - fs_2(i))^(k - 1)));
else
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
a(i) = (fs_2(i) - fs_2(i-1));
b(i) = (1/2)*[(k*C0*((1 - fs_2(i))^(k - 1))) + (k*C0*((1 - fs_2(i-1))^(k - 1)))];
end
SUM(i) = a(i)*b(i);
end
for n = 1:1:100
if n == 1
SUM(n) = a(n)*b(n);
else
SUM(n) = SUM(n-1) + (a(n)*b(n));
end
end
C_avg = SUM;

Réponse acceptée

FScherff
FScherff le 6 Oct 2020
Hello,
Your for-loop is running over 0:0.01:1, resulting in an index-vector [0 0.1 0.2 0.3 ... 0.99 1].That means that already in the first iteration of your foor loop, you are using the value 0 as an array index. But as the error message states, "Array indices must be positive integers or logical values" in Matlab (as you are corrctly using in the second code snippet with n=1:100). That means you should e.g. use 1:101 as index-vecotr of your foor loop, resulting in the same number of iterations, but with valid integer indices. If you really need the values of 0 to 1 in your code (although I don't see that place in the code you posted), use something like
valuesBetweenZeroAndOne = 0:0.01:1;
for i = 1:101
if i == 1
...
else
...
end
%Line where you need the value, e.g. for scaling or something like that
SUM(i) = a(i)*b(i)*valuesBetweenZeroAndOne(i);
end
I have an additional remark:
In your first for-loop you run through 101 iterations (0 to 1 in 0.01-steps), in your second for loop you only have 100 iterations (1 to 100 in 1-steps). As you are using the same vector SUM in booth loops, so I'm relativly sure this is not on purpose.
Greetings!

Plus de réponses (2)

Mario Malic
Mario Malic le 6 Oct 2020
Modifié(e) : Mario Malic le 6 Oct 2020
% i = 0:0.01:1 % 101 element
i = 1 : 1 : 101 % 101 element
You should index arrays/variables with integers.

Walter Roberson
Walter Roberson le 6 Oct 2020
for i = 0:0.01:1
i is first set to 0, then to 0.01, then about 0.02, then about 0.03, and so on.
fs_2(i) = 1 - exp((log((CS_scheil(i)/(k*C0)))/(k-1)));
and there you use i as a subscript for CS_scheil and for the output variable fs_2
First you have to ask what is meant by CS_scheil(i) . It would seem to imply the value of CS_scheil that is associated with the floating point value stored in i . But look at how you created CS_scheil :
FS = linspace(0,0.4079,100);
CS_scheil = k*C0*(1 - FS).^(k - 1);
There might be some ground for saying that you want CS_scheil(i) to mean that value of CS_scheil that is associated with FS having the same value as i, but FS runs 0 to 0.4079 while i runs 0 to 1, so when i > 0.4079 then there would be no obvious meaning to CS_scheil. Furthermore, if you ask for ismembertol(0:0.01:1, FS) you will find that the only intersection between the two, to within reasonable tolerances, is 0 .
Beyond that, you need to solve the problem of indexing with i for the output variable. For that, please read https://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22generalized+for+loop%22

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by