Search code examples
matlabmatrixmatrix-indexing

Matlab: extract submatrix with selecting some values from the last line


      20     4     4    74    20    20  74  85 85 85
 A =  36     1     1    11    36    36  11  66 66 66 
      77     1     1    15    77    77  15  11 11 11
      3      4     2     6     7     8  10  10 15 17

how from the matrix A, I can extract the submatrix whose fourth line (end line) contains only the values ​​[3 6 10]?

for a single value, I do:

B=A(:,A(4,:)==10)

but I do not know how to do this for several values.


Solution

  • Use ismember -

    search_array = [3 6 10]
    subA = A(:,ismember(A(end,:),search_array))
    

    Or bsxfun -

    subA = A(:,any(bsxfun(@eq,A(end,:),search_array(:)),1))