How to run the following codes.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Usman Taya
il 24 Gen 2019
Modificato: Usman Taya
il 24 Gen 2019
what is error in following codes
for i=1:12
if i = 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2;
x = [x1 x2]
end
When i run it it shows following error please help me to solve error.
Array indices must be positive integers or logical values.
0 Commenti
Risposta accettata
Matt Gaidica
il 24 Gen 2019
MATLAB uses one-based indexing. Therefore, in your code, on the first iteration where i = 1, the line marked is trying to index x1 at the zero position:
for i = 1:12
if i == 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2; % <-- error here
x = [x1 x2]
end
There are still issues with your code, but it's hard to know what the fix is without more context. Also, note that i is reserved as an imaginary number in MATLAB, so it's recommended to use ii or another variable as your iterator.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!