Info

This question is closed. Reopen it to edit or answer.

# Solution to Earlier Question on Comparing data and returning indices

1 view (last 30 days)
A on 24 May 2011
Closed: John D'Errico on 6 Oct 2020
Hello! This is not a question, but a reposting of a previous question that I asked with the answer that was supplied by the user Fangjun.
I needed to analyze four sets of data to find the matching number and return the indices of the matching numbers and all of their permutations.
My original data was:
A =
[2,8,10, 14 ,16 , 18, 20, 21, 21, 24, 24, 25, 29 , 33, 34 , 41,
43 , 46 , 46 ,49, 50 ,51 ,58, 58 , 60 ,64, 65, 66, 67, 93 ,96];
B =
[2,6 , 8, 10, 13 ,14 ,15,18 ,20 ,21 , 22, 24 ,25 ,28, 29,30 ,33 ,
34 ,38, 41 ,45 ,46 , 46 ,49 , 50 , 51 ,56 , 58 , 60 ,63 ,64 , 65, 66,67 ,68, 72, 73, 82, 93];
C =
[ 2 , 8, 10 , 14 , 16 ,18 ,20, 21, 21 , 24, 24 , 25 ,29 , 33, 34 ,41 ,
43 ,46 , 46 , 49 ,50 , 51 ,58 ,58 ,60 ,64 , 65 , 66, 67, 93,96];
D =
[2, 8 , 10 , 13 , 14 , 16 , 18, 20 , 21, 21, 24, 24, 25 , 28, 29 ,30 ,33 , 34, 38 ,41, 43 , 46,
49 ,50 , 51 , 58 , 58, 60 ,64, 65 , 66,67 ,68,73, 93, 96];
As an example as what I needed done:
Suppose four short sets of data:
A = [1, 2, 3, 5, 5, 7, 7, 9]
B = [1 ,3 ,6 ,7 ,9]
C = [1, 2, 3, 3, 7, 9]
D = [1, 3, 7, 9, 10, 11]
I would want to return the indices as:
indA = [1, 3, 3, 6, 7, 8]
indB = [1, 2, 2, 4, 4, 6]
indC = [1, 3, 4, 5, 5, 6]
indD = [1, 2, 2, 3, 3, 4]
Fangjun supplied a wonderful solution to this problem:
A = [2,8,10, 14 ,16 , 18, 20, 21, 21, 24, 24, 25, 29 , 33, 34 , 41, 43 , 46 , 46 ,49, 50 ,51 ,58, 58 , 60 ,64, 65, 66, 67, 93 ,96];
B = [2,6 , 8, 10, 13 ,14 ,15,18 ,20 ,21 , 22, 24 ,25 ,28, 29,30 ,33 ,34 ,38, 41 ,45 ,46 , 46 ,49 , 50 , 51 ,56 , 58 , 60 ,63 ,64 , 65, 66,67 ,68, 72, 73, 82, 93];
C = [ 2 , 8, 10 , 14 , 16 ,18 ,20, 21, 21 , 24, 24 , 25 ,29 , 33, 34 ,41 ,43 ,46 , 46 , 49 ,50 , 51 ,58 ,58 ,60 ,64 , 65 , 66, 67, 93,96];
D = [2, 8 , 10 , 13 , 14 , 16 , 18, 20 , 21, 21, 24, 24, 25 , 28, 29 ,30 ,33 , 34, 38 ,41, 43 , 46, 49 ,50 , 51 , 58 , 58, 60 ,64, 65 , 66,67 ,68,73, 93, 96];
Common=intersect(A,B);
Common=intersect(Common,C);
Common=intersect(Common,D);
[A_ind,B_ind,C_ind,D_ind]=deal([],[],[],[]);
for i_Common=1:length(Common)
A_find=find(ismember(A,Common(i_Common)));
B_find=find(ismember(B,Common(i_Common)));
C_find=find(ismember(C,Common(i_Common)));
D_find=find(ismember(D,Common(i_Common)));
for i_A=1:length(A_find)
for i_B=1:length(B_find)
for i_C=1:length(C_find)
for i_D=1:length(D_find)
A_ind=[A_ind,A_find(i_A)];
B_ind=[B_ind,B_find(i_B)];
C_ind=[C_ind,C_find(i_C)];
D_ind=[D_ind,D_find(i_D)];
end
end
end
end
end
Thank you to all the users who helped me answer this question.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by