MATLAB: Compare some parts from one row in two matrices

compare matrices

Hello, I have two matrices, one is
K=[1 4 2 0 1;1 4 0 0 1;2 4 0 0 1;2 4 3 0 1;5 4 2 0 2];
the other is
L=[1 4 0 0 1; 2 4 0 0 1; 4 2 0 0 2].
I want to do somethings:
  1. get one row from L in a loop;
  2. extract the first column and the last column of this row;
  3. compare the extracted number with the related location in every row of K;
  4. If the numbers are matched, get the index from K.Can anyone help me how to do that, please?Thanks in advance.

Best Answer

K_sub = K(:,[1,end]);
L_sub = L(:,[1,end])
[ii,kk] = size(L_sub);
L_sub=reshape(L_sub',[1,kk,ii]);
result = squeeze(sum(bsxfun(@eq,K_sub,L_sub),2) == 2)
Where each column will be a logical index to where your condition is true.