Outpt with zeros when should be values

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

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2016-12-1
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 个评论
Krispy Scripts
Krispy Scripts 2016-12-2
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 2016-12-2
编辑:Image Analyst 2016-12-2
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by