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일
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

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 Exchange에서 Resizing 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