find all values between two indices

조회 수: 29 (최근 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by