如何考虑向量的值在matlab中另一个矩阵的特定值范围内?

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

我有一个大小为 54000 x 2 的向量 A。每一行都包含该行可接受的最小值范围和可接受的最大值范围。例如:

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

另一方面,我有大小为300000 x 1的向量C。现在我想找到向量C的每个值可以放在矩阵A的哪些行中。例如:

C= [1.2; -0.3; 2.4 ]

现在,我需要知道向量 C 的每个值可以位于 A 的哪几行。因此索引的结果可能是这样的:

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

感谢您的帮助

使用bsxfun

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

样本运行:

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

对我来说更直观一点的是:

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

但是,通过使用行操作,计算速度应该会更快:

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