Comparing single element of one array with values of another array

I have an array A which is mx1. I have a second array, B, which is nx2. I would like to find the values of A which fall in between the two values of B in each row.
for k = 1:size(B,1);
[row, col] = find(A>=B(k,1)&&A=<B(k,2));
end
When I run this I run into two problems.
1) A>=B(k,1) (or the other inequality) throws an error about matrix dimension mismatch. I know that I can compare A>=a where a is a single double value, so why does it cause a problem when I am comparing against a single value of the other matrix? Is it possible to work around this without creating a new variable with just that value?
2) I have problems using the && or expressions. I am able to create two different logical variables on either side of the expression (like in the sample code, pretending it works), but when placed together the error, "Operands to the and && operators must be convertible to logical scalar values," is produced. It seems to me that the && and expressions only seem to work with single logical expressions, rather than arrays of logical results. Is this true?

 採用された回答

jonas
jonas 2018 年 7 月 31 日
編集済み: jonas 2018 年 7 月 31 日

0 投票

Some data
A=[1:10]';
B=randi(10,2,10)';
Loop
row=cell(1,10);
for k = 1:size(B,1);
[row{k},~] = find(A>=B(k,1) & A<=B(k,2))
end
1) In this example, I am not getting the first error you described.
2) From what I've understood, && only works on scalars. Personally I rarely use &&, although it's more effecient (stops on first false condition).

1 件のコメント

Bob Thompson
Bob Thompson 2018 年 8 月 1 日
Hmm, yeah I wasn't able to get 1) to repeat either, so I don't know what's up with that. Changing to & instead of && worked well for 2), thanks.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

質問済み:

2018 年 7 月 31 日

コメント済み:

2018 年 8 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by