Effacer les filtres
Effacer les filtres

Index exceeds number of array elements in for loop

1 vue (au cours des 30 derniers jours)
Owen Gray
Owen Gray le 24 Jan 2021
I'm trying to replace 0 with NaN if it is present at the 2nd or 6th element of a column vector. I also need to replace with NaN if there are 9 consecutive zeros, or two NaNs if there are 12 consecutive zeros. I then try to remove all the remaining 0s.
RTa = cell2mat(Rxtcell);
if RTa(2) == 0
if RTa(6) == 0
RTa(6) = NaN
end
RTa(2) = NaN;
end
for n = 1:length(RTa)
X = RTa(n:n+8);
X2 = RTa(n:n+12);
Y = [0;0;0;0;0;0;0;0;0];
Y2 = [0;0;0;0;0;0;0;0;0;0;0;0;0];
if X==Y
if X2==Y2
RTa(n+1) = NaN
end
RTa(n) = NaN
end
end
RT1 = RTa(RTa~=0);
Gives the error
Index exceeds the number of array elements (241).
Error in RTscript (line 11)
X2 = RTa(n:n+12);
If I run script or copy-paste and run in workspace respectively.

Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 24 Jan 2021
Modifié(e) : KALYAN ACHARJYA le 24 Jan 2021
Error message providing sufficient reason for error
As you are trying to access RTa upto n+12, here n is max length(RTa).
Example:
A=[3,4,5,6]
Here length of A is 4, now if you wish to access the data upto 4+any value, is their any sense?

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by