How to make a loop that will find common elements between many arrays of strings, and omit the arrays that have already been compared?

2 views (last 30 days)
Gemma Shastri on 20 Oct 2021
Commented: Gemma Shastri on 29 Oct 2021
I have many arrays of the same length of 100 string values. I need to compare each array with each subsequent array, have the similar values printed, and then have the same comparison done with all the other arrays. This is what I have so far:
T = Table
T{:,:}; % creates array from table T
A= table2array(T(1,:));
B= table2array(T(2,:));
C= table2array(T(3,:));
D= table2array(T(4,:));
E= table2array(T(5,:));
F= table2array(T(6,:));
G= table2array(T(7,:)); % sets each row to individual array
I = intersect(A,B) % intersection between 2 arrays (rows)

Navya Singam on 26 Oct 2021
Hi Gemma,
The following code helps in converting the table to array of M*N with each row corresponding to the table row
A = table2array(T); %%T is a table, and A is an array where each row corresponds to each row in table
sizeOfArray = size(A); %% sizeOfArray holds the size of the array A
The following code helps in the looping of the array row wise and finding the similar strings
for i = 1:sizeOfArray(1) %% i runs from 1 to last row
for j = i+1:sizeOfArray(1) %% j runs from i+1 to last row
common = intersect(A(i,:),A(j,:));
disp("Comparing array "+i+" and array "+j)
if numel(common)==0
disp("No Matching elements")
else
disp(common)
end
end
end