MATLAB: Outpt with zeros when should be values

intervalzeros

I have a matrix of two columns. The first column is cue times, as in times an animal was cued to do something. The second column is times that the animal responded. If the animal correctly responded then the animal will respond within a range greater than .15 seconds of cue and less then 5 seconds from cue. If so this is a correct behavior and needs to go into one matrix(correcttrials), if not then this is an error behavior and needs to go in a second matrix(errortrials).
The original Matrix (I have also attached it):
intervals =
81.4358 735.1798
272.0970 1442.0839
515.2985 1682.2252
575.41885 1961.58675
734.95985 2507.14965
915.20085 2681.83055
1319.4032 2682.0705
1441.0439 2777.6710
1526.62435 3498.9947
1681.4852 0
1961.38675 0
2087.0074 0
2372.6089 0
2559.9099 0
2681.73055 0
2838.63135 0
3049.3324 0
3140.97285 0
3322.3738 0
3498.87465 0
This is the code I have written, but I am not sure why the correct output has zeros in it and the error output gets all zeros.
Here is my code:
data=intervals;
cue=data(:,1);
response=data(:,2);
for k=1:length(cue)
for i=1:length(response)
if reward(i)>= (cue(k)+.15) && reward(i)<= (cue(k)+5)
correcttrials(i)=response(i);
else
errortrials(i)=response(i);
end
end
end
Here are the Matrices I am getting out:
Correct Trials:
correcttrials=
735.1798 1442.0839 1682.2252 1961.58675 735.1798 0 2682.0705 1442.0839 0 1682.2252 1961.58675 0 0 0 2682.0705
Error Trials:
errortrials =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I am intending to get:
Correct Trials:
correcttrials =
735.1798 1442.0839 1682.2252 1961.58675 2682.0705
Error Trials:
errortrials =
2507.14965 2681.83055 2777.6710 3498.9947

Best Answer

intervals = [...
81.4358 735.1798
272.0970 1442.0839
515.2985 1682.2252
575.41885 1961.58675
734.95985 2507.14965
915.20085 2681.83055
1319.4032 2682.0705
1441.0439 2777.6710
1526.62435 3498.9947
1681.4852 0
1961.38675 0
2087.0074 0
2372.6089 0
2559.9099 0
2681.73055 0
2838.63135 0
3049.3324 0
3140.97285 0
3322.3738 0
3498.87465 0];
d = intervals;
dn0 = d(d(:,2)~=0,2)';
da = abs(d(:,1) - dn0);
t = any(da > .15 & da < 15);
correcttrials = dn0(t)
errortrials = dn0(~t)