Get sum of similarites between rows

Hi Guys,
I have one matrix and i want to compare each value of each row to other rows and then get the sum of similarities between the two rows, for example a = [ 1 2 3 4; 2 4 3 6; 4 2 3 9] So, the output should be for the comparison of the first two rows is 1 as the first row and second row have only one value similar in the third column for comparison between first row and third row will be 2 for comparison between second row and third row will be 1
Many thanks,

Answers (2)

a = [ 1 2 3 4; 2 4 3 6; 4 2 3 9];
N_Row=size(a,1);
Result=zeros(N_Row,N_Row-1);
for k=1:N_row
for j=k+1:N_row
Result(k,j)=sum((a(k,:)-a(j,:))==0);
end
end
out = nonzeros(triu(sum(bsxfun(@eq,permute(a,[1 3 2]),permute(a,[3 1 2])),3),1))
or
n = nchoosek(1:3,2)
out = arrayfun(@(i1)nnz(a(n(i1,1),:) == a(n(i1,2),:)),(1:size(n,1))')

Categories

Asked:

on 30 Nov 2011

Community Treasure Hunt

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

Start Hunting!