简体   繁体   中英

How to consider the value of a vector is in specific range of values of another matrix in matlab?

I have a vector A with size of 54000 x 2. Each row includes range of accepted min and accepted max of values for that row. For example:

A=[0.5 1.5 ; 1 2.5; -0.5 1.5]

From the other side, I have vector C with size of 300000 x 1. Now I want to find that each value of vector C can be placed in which rows of Matrix A. for example:

C= [1.2; -0.3; 2.4 ]

Now, I need to know that each value of vector C can be located in which rows of A. So the result of indices could be like this :

   c_indx(1,1)= [1,1,1]
   c_indx(2,1)= [0,0,1]
   c_indx(3,1)= [0,1,0]

THX for your help

A bit more intuitive to me would be:

c_indx = bsxfun(@le,A(:,1).',C) & bsxfun(@ge,A(:,2).',C);

However, by making use of row-operations, it should be computationally faster like this:

c_indx = cell2mat(arrayfun(@(x)(A(:,1)<=x & A(:,2)>=x).',C,'UniformOutput',false))

Using bsxfun

out = all(cat(3,bsxfun(@le,C(:),A(:,2).'),bsxfun(@ge,C(:),A(:,1).')),3);

Sample run:

A = [0.5 1.5 ; 1 2.5; -0.5 1.5];
C= [1.2; -0.3; 2.4];

>> out

out =

 1     1     1
 0     0     1
 0     1     0

A=[0.5,  1.5 ;  1,   2.5; -0.5,  1.5];

C= [1.2; -0.3;  2.4;  1.7;  0.3;  -0.6;  1.1];

>> out

out =

 1     1     1
 0     0     1
 0     1     0
 0     1     0
 0     0     1
 0     0     0
 1     1     1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM