Comparing single element of one array with values of another array

3 views (last 30 days)
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?

Accepted Answer

jonas
jonas on 31 Jul 2018
Edited: jonas on 31 Jul 2018
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 Comment
Bob Thompson
Bob Thompson on 1 Aug 2018
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!