Azzera filtri
Azzera filtri

Return row numbers of look up matrix

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 6 Ago 2016
Modificato: Azzi Abdelmalek il 6 Ago 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 Commento
Steve Fox
Steve Fox il 6 Ago 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!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by