Using find command on each term in a vector
4 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
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 件のコメント
Honglei Chen
2012 年 2 月 16 日
@James, you are right, I forgot that part, I've updated the answer for future reference.
その他の回答 (2 件)
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
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?
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)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!