Clear Filters
Clear Filters

Index exceeds number of array elements in for loop

1 view (last 30 days)
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.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 24 Jan 2021
Edited: KALYAN ACHARJYA on 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?

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!