MATLAB: Create a matrix from columns of larger matrix with specific numbers

create smaller matricesselecting columns

I can't get the code below to work. I want create a matrix D from the columns in M that contain a specific number in row 2 of M. For example
M=[1 2 3 4 5 ; 1 3 2 1 3]
colId = any(M(2,:) 1)
D = M(:,colid)
For the numbers 1 in the second column the answer should be
D=[1 4 ;1 1 ]
Any help appreciated

Best Answer

D = M(:,M(2,:)==1)