MATLAB: Combine different matrices to create a new one

combine different matrices to create a new one

I want to create matrix C, from matrix A and B.
A = (r1 r2 r3)
B=
[r1 r2
r3 r4
r2 r1
r5 r6
r2 r3
r3 r2
r3 r1
r1 r3]
C=(s1 s2 s3)
Matrix A and matrix C have the same size.
s1, s2, s3 are row’s number in matrix B. The row’s number in matrix B start from 0.
s1= bigger row’s number between r1 r2 and r2 r1
s2= bigger row’s number between r2 r3 and r3 r2
s3= bigger row’s number between r3 r1 and r1 r3
this is an example:
A= [4 1 6]
B= [4 1
6 8
1 6
1 4
6 1
6 4
4 6]
C= [3 4 6]
Any help would be appreciated.
Thank you very

Best Answer

A= [4 1 6]
B= [4 1
6 8
1 6
1 4
6 1
6 4
4 6]
K = permute(hankel(A(1:2),[A(2:end),A(1)]),[3,1,2]);
P = repmat(B,1,1,numel(A));
[L1,~] = find(all(bsxfun(@eq,K,P),2));
[L2,~] = find(all(bsxfun(@eq,K(:,end:-1:1,:),P),2));
C = max([L1,L2].') - 1