Effacer les filtres
Effacer les filtres

Return row numbers of look up matrix

2 vues (au cours des 30 derniers jours)
Steve Fox
Steve Fox le 6 Août 2016
Commenté : Steve Fox le 6 Août 2016
In my real data set I'm looking to return the row number references between two data set arrays for date ranges. Here's a simple example using matrices:
x = [10 20;21 30; 31 40];
y = [35;15];
Desired result [3;1] indicating that rows 3 and 1 of x meet the criteria of y being between x(:,1) & x(:,2)
[C,ia,ib] = find(y>=x(:,1) & y<=x(:,2));
[C,ia,ib] = find(ismember(y>x(:,1) & y<x(:,2),x));
I want to find where y values are between which x values without using a for loop. In my real data set I have 7MM rows so ideally I wouldn't loop through every row to save time.
I tried various combinations of 'find' and 'ismember' but can't seem to figure out how to get the result.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 6 Août 2016
Modifié(e) : Azzi Abdelmalek le 6 Août 2016
x = [10 20;21 30; 31 40];
y = [35;15];
a=bsxfun(@ge,y',x(:,1))&bsxfun(@le,y',x(:,2));
[out,~]=find(a)
  1 commentaire
Steve Fox
Steve Fox le 6 Août 2016
That's a great answer and works perfectly on my data set. Though it's only 3 lines, it's brilliant and short. I still need to get my head around the first line. I've never used bsxfun nor ge/le functions. Many thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by