How can I fix "Index in position 2 exceeds array bounds" in for loop/ if statements?
Mostra commenti meno recenti
W=cell(p,1);
for j=1:(p-1);
w=A(index(j):index(j+1)-2);
W{j}=w;
end
W2=cell(p,1);
for j=1:(p-1);
w2=A(index(j):index(j+1)-2);
W2{j}=w2;
end
Z=cell(p,1);
for ii=1:p
P1=W{ii,1};
Z{ii,1}=P1(3:3:end);
end
for ii=1:p
P2=Z{ii,1};
if P2(end,1)>0
W2(ii,1)={[]};
end
end
I am getting the error "Index in position 2 exceeds array bounds" on the line if P2(end,1)>0
The W2 matrix looks how I want it to look I just dont know how to stop it from producing an error.
4 Commenti
Sindar
il 6 Nov 2020
try debug mode
dbstop if error
and figure out whether everything has the sizes you expect. It looks like P2 is empty, but it's not obvious where that happened
Specifically, it would be useful to know the following at the time of the error:
- the value of ii
- the size of P2 (my guess is that it is an empty array)
- the size of W{ii,1}
Extra question: do you use Z for anything later, or is it purely a temporary variable in this chunk?
Andrew Stark
il 6 Nov 2020
Andrew Stark
il 6 Nov 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!