필터 지우기
필터 지우기

Return row numbers of look up matrix

조회 수: 2 (최근 30일)
Steve Fox
Steve Fox 2016년 8월 6일
댓글: Steve Fox 2016년 8월 6일
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.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 6일
편집: Azzi Abdelmalek 2016년 8월 6일
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
Steve Fox
Steve Fox 2016년 8월 6일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by