フィルターのクリア

How do I find the index of a value in an array that lies between two values in another array?

14 ビュー (過去 30 日間)
I have two arrays of numbers, both of which are increasing:
EX:
a = [10 20 30 40 50];
b = [5 13 26 44 55];
I want to run a loop by using a value in a and the following value in b (i.e., 10 from a and 13 from b). But to make sure it is the following value from b, I want to check to make sure a1 < b < a2. So on the next iteration I would want to use 20 from a and 26 from b, since 26 is greater than 20 but less than 30. BUT if there isn't a corresponding number (i.e. 30 from a, nothing from b since 44 is larger than 40), I need to skip that number for the current iteration.
Any advice on how to find the correct value in b by comparing it to two numbers in a? And I want to be able to find that value, in case it's somewhere in the array I'm not expecting. I'm not super experienced in MATLAB so maybe this is stupid easy, but I'm having trouble and would much appreciate any help.

採用された回答

Iain
Iain 2013 年 7 月 30 日
for i = 1:numel(a)-1
c = b > a(i) & b < a(i+1);
c = b(c);
if isempty(c)
continue
else
% do something, since c has a value in it.
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by