필터 지우기
필터 지우기

Finding the n'th element satisfying a condition

조회 수: 36 (최근 30일)
Nirmal
Nirmal 2012년 5월 7일
I have a huge array A (about 30 million elements) and I'm trying to extract the n'th element satisfying a condition A == b. Currently, I' using the following:
x = max(find(A == b,n))
Is there a more efficient way to do this since I need to do this several times in my code?
Thanks.

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 5월 7일
That looks good to me. The only thing that might make it faster would be to index instead of use max():
idx = find(A==B,n,'first')
idx(end)
  댓글 수: 1
Nirmal
Nirmal 2012년 5월 7일
Using this made the code considerably faster, actually..

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

추가 답변 (2개)

Thomas
Thomas 2012년 5월 7일
Seems to be the fastest way of doing this.. Index might make it faster (though I doubt it..) try timing it..

Walter Roberson
Walter Roberson 2012년 5월 7일
I am not sure why you are using max() ?
t = find(A == b, n, 'first');
t(end)
If you are doing this repeatedly, what is varying, and what is known at the time you want to start the first search? If you know all the values you will be searching on, then the three-output version of ismember() might be appropriate.
  댓글 수: 1
Nirmal
Nirmal 2012년 5월 7일
Thanks. I'll also try out the ismember. Avoiding max helped.

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

카테고리

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