Index exceeds number of array elements in for loop

1 次查看(过去 30 天)
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.

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-1-24
编辑:KALYAN ACHARJYA 2021-1-24
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?

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by