Get sum of similarites between rows
Afficher commentaires plus anciens
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,
Réponses (2)
Fangjun Jiang
le 30 Nov 2011
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
Andrei Bobrov
le 30 Nov 2011
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))')
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!