Error: Attempted to access E(-251); index must be a positive integer or logical
1 view (last 30 days)
Show older comments
Hi,
I have a function that gives this error. I did not write this code nor do I have anything more then a novice understanding of MATLAB code. Unfortunately, I do not know anyone with the expertise to help me troubleshoot. I've tried messing with abs on the equation in question that gives the error but then the error reads A(-251) and if I abs both of them I get 0. Any help would be greatly appreciated. Thanks!
function E = findevents(motion,fps,win)
frames = length(motion);
L = ceil(fps*win); % 10 second window
d = 1; % Displace by 1 frame
E = (10*ones(frames,1));
A = (10*ones(frames,1));
for i=floor(L/2):d:frames-(floor(L/2)-d)
snippet = (motion(i-floor(L/2)+1:i+floor(L/2)-d));
deviation = (snippet-mean(snippet)).^2;
E(i) = sqrt(mean(deviation));
A(i) = mean(snippet);
end
% E(A>=.5) = .5;
Error in findevents (line 11) E(i) = (sqrt(mean(deviation)));
0 Comments
Answers (1)
Thorsten
on 28 May 2015
In Matlab the indices have to be integer numbers > zero. So if you use
E(i) = sqrt(mean(deviation));
A(i) = mean(snippet);
you have to ensure that i is always > 0 and integer.
You could probably rewrite the code as follows:
ind = floor(L/2):d:frames-(floor(L/2)-d);
for i=1:numel(ind)
ii = ind(i);
snippet = (motion(ii-floor(L/2)+1:ii+floor(L/2)-d));
deviation = (snippet-mean(snippet)).^2;
E(i) = sqrt(mean(deviation));
A(i) = mean(snippet);
end
5 Comments
Walter Roberson
on 1 Jun 2015
Your Location and Location2 are the same size so it makes sense to test them with == to get back an array of the same size. But your evnts is a completely different size, about 504 entries larger. For any given Location(J), which evnts should be correspond?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!