Filter löschen
Filter löschen

Return row numbers of look up matrix

2 Ansichten (letzte 30 Tage)
Steve Fox
Steve Fox am 6 Aug. 2016
Kommentiert: Steve Fox am 6 Aug. 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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 6 Aug. 2016
Bearbeitet: Azzi Abdelmalek am 6 Aug. 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 Kommentar
Steve Fox
Steve Fox am 6 Aug. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by