필터 지우기
필터 지우기

Vectorizing finding indexes

조회 수: 3 (최근 30일)
Sargondjani
Sargondjani 2012년 6월 24일
hi, i have a vector X and a vector Y (both in ascending order). for every element in X i want the indexes of the values in Y between which this element lies. Actually, 1 index suffices (preferably the higher bound).
For example if have
Y=[0, 1, 2, 3, 4, 5]
X=[0.1, 2.5, 2.8, 4.1];
then I want to get as a result:
IND = [2 4 4 6]; %the higher bounds of the interval in Y in which the elements of X fall
I can do this with a for loop:
for ix=1:length(X);
IND(1,ix)=min(find(Y>X(ix)))
end
My question is whether it is possible to vectorize this, and how... Many thanks in advance!

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 24일
[counts, tIND] = histc( X, Y );
IND = tIND + 1; %to get the higher index
Note that this has questionable results in the case where an X is exactly equal to a Y: it will return the bin number of the next Y. You could, though, correct for this with
t = (X == Y(tIND));
IND(t) = IND(t) - 1;
It is also possible to correct for it at the time of histc(), but the code becomes notably more difficult to read.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 6월 24일
While you are thinking about that, also think about what to do if an X is before or after all of the Y.
See the comments in http://www.mathworks.com/matlabcentral/answers/41814-interpolation-with-high-frequency-financial-data
Sargondjani
Sargondjani 2012년 6월 24일
that's not going to happen. i am doing value function iteration using Y as the grid (for the state variable), and i force my policy function, X, to be in/on the grid

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jamie Rodgers
Jamie Rodgers 2012년 6월 24일
Try This: Your vector
Y=[0:1:1000];
X=[0.1, 2.5, 2.8, 4.1];
Vectorised code
Z1=arrayfun(@(a)intersect(Y,ceil(a)),X);
idx=arrayfun(@(x)find(Y==x),Z1);
  댓글 수: 1
Sargondjani
Sargondjani 2012년 6월 24일
this probably works best, because i would actually like to have index of lower bound and index of higher bound... this way i can get them both directly with an understandable method :-)

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2012년 6월 24일
[idx,~] = find(bsxfun(@eq,ceil(X(:)'),Y(:)))
  댓글 수: 1
Sargondjani
Sargondjani 2012년 6월 24일
i really like this but if i change small thing i get results that i dont get!
if i remove the transpose (i would prefer the solution to have the same dimension as X and Y) then i get a weird result
and i get only ones if i remove the (:)
...so unfortunately this solution is probably out of my league

댓글을 달려면 로그인하십시오.


Sargondjani
Sargondjani 2012년 6월 24일
Hmmmmm, andrei and jamie: do your solutions only work when Y consists of integers??? In my example Y was just integers, but in fact Y is not integers...
when i tried your solutions with Y not being integers i got results that were not correct
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 6월 24일
You are correct: the expressions given by Jamie and Andrei as of the time of my writing this comment, only work for integer Y. My histc() based code does not depend on Y being integer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by