find all values between two indices
29 次查看(过去 30 天)
显示 更早的评论
I have two index values that I want to find the range in between in matrix A (indx1 and indx2). Indx1 happens before indx2 (so finding the values between them should be straight forward). But, there are occasions where indx1 does not have a corresponding indx2 (i.e. if the code could search matrix A it would first run into an indx1 and then look for the first indx2, but no indx1 could come before indx2 (this would be a mis-trial). If that happened, it should just look at the next indx1 and start again looking for indx2, then pull all the values into a cell array. My question is how do a code this?!
For perspective, these are reaching movements where only a percentage of trials are rewarded (indx2). Indx1 is the start of the pull. I want the values that correspond to rewarded pulls.
thanks for any help!
2 个评论
回答(1 个)
Image Analyst
2014-4-5
I'm not sure we understand the description. To get all the values of A between index1 and index2, use
theValuesBetween = A(index1:index2);
If either is not defined, that's bad (but can be handled with exist()). I'd recommend you initialize them to null
index1 = [];
index2 = [];
theValuesBetween = [];
% Then do your search for index1 and index2.
% If one or the other is not found, it will still be null instead of some numerical value.
% So check for that.
if isempty(index1)
uiwait(warndlg('Error: index1 was not found!'));
elseif isempty(index2)
uiwait(warndlg('Error: index2 was not found!'));
elseif isempty(index1) && isempty(index2)
uiwait(warndlg('Error: neither index1 nor index2 was not found!'));
else
% SUCCESS! Both index1 and index2 were found.
theValuesBetween = A(index1:index2);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!