how to compare every pair of rows and return a matrix?

조회 수: 1 (최근 30일)
chocho
chocho 2018년 5월 22일
댓글: Jan 2018년 5월 28일
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.
I have attached some rows from my matrix to make the question more clear
  댓글 수: 4
Guillaume
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.
chocho
chocho 2018년 5월 22일
편집: chocho 2018년 5월 22일
@Guillaume sorry is a typo I mean row2 and row3 because they have 5 genes in common (RASGRP4, RASGPR2, RAF1, PRAKACA, RAS1) so the output is 5

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
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
chocho
chocho 2018년 5월 28일
@ Jan hello jan plz if I want to change new_variable = v / (m + n) to new_variable = v^2 / (m * n) So this line will be like this :
v= sum(ismember(Sub{i1}, Sub{i2}))^2 / (len1 * len2);
is it right ??
Jan
Jan 2018년 5월 28일
Yes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by