Borrar filtros
Borrar filtros

Return row numbers of look up matrix

2 visualizaciones (últimos 30 días)
Steve Fox
Steve Fox el 6 de Ag. de 2016
Comentada: Steve Fox el 6 de Ag. de 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.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 6 de Ag. de 2016
Editada: Azzi Abdelmalek el 6 de Ag. de 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 comentario
Steve Fox
Steve Fox el 6 de Ag. de 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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by