MATLAB: How to calculate max. matrix row number

calculate max. matrix row number

I have A and B matrices. I would like to write a code to calculate Max. row' number between 1 4 and 4 1.
Matrix A has 300 rows with 3 columns. Matrix B has 500 rows and 2 columns. row's number in Matrix B start from 0.
A= [4 1 6]
B= [4 1
1 6
1 4
6 1
6 4
4 6]
max row's number between 1 4 and 4 1 = 2
please help me with this issue, if you know how to solve this.
thanks

Best Answer

m = size(A,2);
Y1 = reshape(permute(cat(3,A,circshift(A,[0 -1])),[3,2,1]),size(A,1),[]); %
Y2 = flip(Y1,1);
Bp = permute(B,[2,3,1]);
X = reshape(any([all(bsxfun(@eq,Y1,Bp));all(bsxfun(@eq,Y2,Bp))]),m*2,[])';
Z = reshape(max(bsxfun(@times,X,(1:size(B,1))')),m,[])'-1;