How to compare two vectors with different dimensions to get logical array?

2 ビュー (過去 30 日間)
Lei
Lei 2012 年 5 月 3 日
Hello, I have two vectors with different dimensions. To make it simple,lets say A=[775.4; 770.2; 690;...] is n by 1 and B is m by 2: for example, B(1,:)=775.446,774.706. Basically, I want this: if A(i,1)<=B(i,1) & A(i,1)>=B(i,2) is true,then do sth. i tried to use for statement such as for i=size(A),obviously, theres problem because of the two dimensions. If anybody knows how to solve this problem, please let me know. Thanks.
  3 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 5 月 3 日
Please provide the expected result for your simple example.
Lei
Lei 2012 年 5 月 3 日
Hi Sean,
Thanks for your help!
Yes,I want to do this for each value of A.
What I want is: if each of the value of A is in the range of row of B ( because each row of my B has two values). Then I do sth else. The reason i did not put the other code is here is that they look complicated.
In my case, my A is 700 by 1 and my B is 30 by 2.
Any of your help will be appreciated!

サインインしてコメントする。

採用された回答

Jan
Jan 2012 年 5 月 3 日
A = rand(10, 1);
B = rand(20, 2);
n = min(size(A, 1), size(B, 1));
R = A(1:n) <= B(1:n, 1) & A(1:n) >= B(1:n, 2);
for i = 1:n
if R(i)
...
end
end
Or:
n = min(size(A, 1), size(B, 1));
for i = 1:n
if A(i) <= B(i, 1) & A(i) >= B(i, 2)
...
end
end
  2 件のコメント
Lei
Lei 2012 年 5 月 3 日
Hi Jan,
Thanks for your help!
It works well!
Lei
Lei 2012 年 5 月 3 日
Hi Jan,
What if the size of A is larger than the size of B?
What can I do to solve this problem? Actually,I did not notice this until i tried to run my own code.

サインインしてコメントする。

その他の回答 (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