Using find command on each term in a vector

조회 수: 4 (최근 30일)
James
James 2012년 2월 16일
편집: Matt J 2013년 10월 14일
I have two vectors that I am trying to use in my calculation.
One is a list of coordinates. A=-10:10
The other is a list of random numbers that may or may not be between these values. B=22*rand(1,30)-11
What I am trying to do is find the first value in A that is greater than each term in B. If B(1)=6.4, then I want the find command to give me the index in A that corresponds to 7. I want this value for each term in the B array, even if it’s the “empty 1-by-0 matrix.”
The syntax for that on each individual term in B is easy enough, but my question is: Is there a way to use the find command on the entire vector B at the same time and store that result in a third vector, or do I need to use using a for C=1:size(B,2) loop?

채택된 답변

Honglei Chen
Honglei Chen 2012년 2월 16일
Not really a syntax for find, but take advantage of arrayfun
a = -10:10;
b = 22*rand(1,30)-11;
c = arrayfun(@(x) find(a>x,1), b, 'UniformOutput', false)
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2012년 2월 16일
Welcome to the bsxfun club!
Honglei Chen
Honglei Chen 2012년 2월 16일
@James, you are right, I forgot that part, I've updated the answer for future reference.

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

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 2월 16일
A=-10:10
B=22*rand(1,30)-11
out = find(all(bsxfun(@gt,A.',B),2),1,'first')
  댓글 수: 2
Honglei Chen
Honglei Chen 2012년 2월 16일
Hi Andrei, it is an interesting thought to use bsxfun, but it doesn't seem to give the right answer. Am I missing something?
James
James 2012년 2월 16일
I tried this, and got an empty matrix. The bsxfun(@gt,A.',B) part worked fine, but the all(bsxfun(@gt,A.',B),2) gave me a column of zeroes. Since there were no ones in the matrix, the find part of this command gave me an empty mtarix.

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


Sean de Wolski
Sean de Wolski 2012년 2월 16일
I don't see why you need to compare every value. Only compare values to the max of B since that is a requirement.
A=-10:10;
B=22*rand(1,30)-11;
idx = find(A>max(B),1,'first');
A(idx)
  댓글 수: 1
James
James 2012년 2월 16일
If I'm understanding what your command does (and I may be wrong on this, since I'm a relative MATLAB newbie), it compares the max value of B to each value in A. If the maximum value of B is greater than everything in A (which is quite possible) then you'll end up with idx being an empty matrix because no values will meet the criteria.
If the first four values of B are 6.4, -10.2, -1.3, 10.6, what I would like are the indices in A that correspond to 7, -10, -1 and 11 repsectively. Since there is no 11 in A, the output value for that index would end up being an empty matrix, so the output for these values would be [18 1 10 [] ]
(As an aside, I just used these values of A and B for simplicity. Sadly, my real data set is not this neat so I can't use floor and ceil.)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by