Outpt with zeros when should be values

1 view (last 30 days)
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
  1 Comment
Image Analyst
Image Analyst on 1 Dec 2016
We can't run this code because you have not defined the reward array.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 2 Dec 2016
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)
  2 Comments
Krispy Scripts
Krispy Scripts on 6 Dec 2016
If I wanted to instead get the values in column one that meet the criteria instead of column 2 could that be easily done?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 1 Dec 2016
Try this vectorized way:
load('intervals.mat');
cueTimes = intervals(:,1); % Extract the first column.
responseTimes = intervals(:,2); % Extract the second column.
% Get the time differences.
timeDifferences = responseTimes - cueTimes
% Convert from milliseconds to seconds. ??????????
timeDifferences = timeDifferences / 1000;
correctTrialsRows = (timeDifferences > 0.15) & (timeDifferences < 5)
correctTrials = responseTimes(correctTrialsRows)
errorTrials = responseTimes(~correctTrialsRows)
None of the things was correct so I figured the times might be in milliseconds whereas your criteria was in seconds, so I converted. I'm still not sure if this gives you what you want though.
  2 Comments
Krispy Scripts
Krispy Scripts on 2 Dec 2016
It does not need to be converted into milliseconds. It also cannot be assumed that the value in one column corresponds to another. So thats why I did not look to get a time difference.
My method was to take each value in the first column, for your code cueTimes, and read through the entire second column. If any value is within the cutTime window, greater than .15 and less than 5 (seconds), than put that second column responeTime in correctTrials. If it does, not then it should go in the errorTrials.
Does that help at all? I am not sure about how to write for this though..
Image Analyst
Image Analyst on 2 Dec 2016
Edited: Image Analyst on 2 Dec 2016
Then none of your time differences are in the range 0.15 to 5 seconds. Let's take a typical start/stop pair - the pair in row #5. Start/cue time of 734.95985 seconds, and response (stop time) at 2507.14965 seconds. That's 1771 seconds. And all the rest are like that. Not one of them is in the range 0.15 to 5 - they're all hundreds.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!