Will this do what you want? Note the requirement that A and B are sorted, which you have...
A=[1, 2, 9, 11, 14, 17, 18, 19, 32, 34, 38, 42, 42, 46, 53, 56, 57, 62, 64, 95, 96, 96]
Aidx = 1:length(A);
B = [2, 5, 9, 11, 14, 17, 18 , 19, 26, 32, 34, 38, 42, 53, 56, 57, 62, 64, 65, 81, 96]
Bidx = 1:length(B);
I = ismembc(A,B);
Aintrsct = A(I)
Aidxintrsct = Aidx(I)
For B, then do:
I = ismembc(B,A);
Bintrsct = B(I)
Bidxintrsct = Bidx(I)
%
%
%
%
%
%
EDIT Now that we have expected output...
A = [1, 2, 9, 11, 14, 17, 18, 19, 32, 34, 38, 42, 42, 46, 53, 56, 57, 62, 64, 95, 96, 96]
Aidx = 1:length(A);
B = [2, 5, 9, 11, 14, 17, 18 , 19, 26, 32, 34, 38, 42, 53, 56, 57, 62, 64, 65, 81, 96]
Bidx = 1:length(B);
I = ismembc(A,B);
Aidxintrsct = Aidx(I)
Bidxintrsct = arrayfun(@(x) Bidx(x==B),A,'Un',0);
Bidxintrsct = [Bidxintrsct{:}]
Best Answer