find all values between two indices

28 views (last 30 days)
MacKenzie
MacKenzie on 4 Apr 2014
Answered: Image Analyst on 5 Apr 2014
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!

Answers (1)

Image Analyst
Image Analyst on 5 Apr 2014
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

Community Treasure Hunt

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

Start Hunting!