how to compare every pair of rows and return a matrix?
6 ビュー (過去 30 日間)
古いコメントを表示
Hello guys, I have a matrix of 135 rows and 2 columns and I want to compare every two pairs of rows based on their overlapped elements and return a matrix.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190739/image.png)
I have attached some rows from my matrix to make the question more clear
4 件のコメント
Guillaume
2018 年 5 月 22 日
編集済み: Guillaume
2018 年 5 月 22 日
It's not clear what a common element is. Not helped by the fact that there's probably a typo in if i take row2 and row2 they have 5 common elements since comparing something with itself is certain to say that everything is common.
Similarly, defines what compare means for you.
採用された回答
Jan
2018 年 5 月 22 日
I guess your matrix is a cell string and you want to find common sub-strings in it.
% Assuming your matrix is called "M":
n = size(M, 1);
% Convert the strings in the 2nd column to cell strings:
Sub = cell(1, n);
for k = 1:n
Sub{k} = strtrim(strsplit(M{k, 2}, ','));
end
Match = cell(n, n);
for i1 = 1:n
for i2 = i1+1:n
v = intersect(Sub{i1}, Sub{i2});
Match{i1, i2} = v;
Match{i2, i1} = v; % Output is symmetric
end
end
Or maybe you want ismember instead of intersect. Or you want to output the indices? Or the output should be a logical matrix, which is TRUE if the sub-strings have any overlap?
Please explain "compare every two pairs of rows" specifically. Most likely the above code can be adjusted easily.
8 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!