What’s the proper way of checking if the first occurrence any element from a vector exists in a matrix? For example if I have
A = [1, 3]
and
B = [ 1, 2 ;
1, 4 ;
2, 3 ;
2, 4 ;
3, 4 ];
I should get something that returns the indices of matrix B where this condition is met. So for the example I should get.
indx = [1, 1]
I’m using MATLAB R2012a
In order to search for all elements of vector
Ain matrixByou may usebsxfun:This comparison disregards the matrix shape of
Band treats it as a stack of elements. In your exampleBhas10elements andAhas2, thereforetmpis a binary matrix of size10x2withtruewhereverBequals an elements ofA.To find the first element of
Bthat equals any element ofAyou simply doTo convert the linear index
idxinto a row-col pair intoBCheers!