Reputation: 439
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
Upvotes: 1
Views: 68
Reputation: 699
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))
Upvotes: 2
Reputation: 3898
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
Upvotes: 1