Lei
Lei

Reputation: 45

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

I have two vectors with different dimensions. To make it simple,lets say

A = rand(30, 1);
B = rand(10, 2);

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.

Upvotes: 0

Views: 1171

Answers (2)

George
George

Reputation: 317

You might want to do something like this

for i = min(size(A), size(B))
   if  A(i,1)<=B(i,1) & A(i,1)>=B(i,2)

then do stuff.

Not really familiar with Matlab, and to lazy to start it; hope it helps.

Upvotes: 1

Jonas
Jonas

Reputation: 74940

You can do the following

%# find out which array is longer
lenA = size(A,1);
lenB = size(B,1);

minLength = min(lenA,lenB);

%# do vectorized comparison
trueOrFales = all(A(1:minLength)<=B(1:minLength,1)) && ...
    all(A(1:minLength)>=B(1:minLength,2))

Upvotes: 2

Related Questions