find all values between two indices

27 ビュー (過去 30 日間)
MacKenzie
MacKenzie 2014 年 4 月 4 日
回答済み: Image Analyst 2014 年 4 月 5 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 4 日
What is the question?
MacKenzie
MacKenzie 2014 年 4 月 5 日
how do I code this

サインインしてコメントする。

回答 (1 件)

Image Analyst
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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by