Error: Attempted to access E(-251); index must be a positive integer or logical

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

Answers (1)

Thorsten
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
Arnold G
Arnold G on 1 Jun 2015
Edited: Arnold G on 1 Jun 2015
Thanks for your assistance. As advised, I reran the file. The output was as follows:
Name Size Bytes Class Attributes
evnts 7398x1 59184 double
thr 1x1 8 double
Location 6894x1 27576 single
Location2 6894x1 27576 single
Additionally, I have shared the file in question. Also, if it would better to repost or post elsewhere please let me know. Thanks!
Walter Roberson
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!