How to make a loop that will find common elements between many arrays of strings, and omit the arrays that have already been compared?
1 回表示 (過去 30 日間)
古いコメントを表示
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)
0 件のコメント
回答 (1 件)
Navya Singam
2021 年 10 月 26 日
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
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!