Vectorizing finding indexes
Ältere Kommentare anzeigen
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!
Akzeptierte Antwort
Weitere Antworten (3)
Jamie Rodgers
am 24 Jun. 2012
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 Kommentar
Sargondjani
am 24 Jun. 2012
Andrei Bobrov
am 24 Jun. 2012
[idx,~] = find(bsxfun(@eq,ceil(X(:)'),Y(:)))
1 Kommentar
Sargondjani
am 24 Jun. 2012
Sargondjani
am 24 Jun. 2012
0 Stimmen
1 Kommentar
Walter Roberson
am 24 Jun. 2012
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.
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!