Intersection of matrix rows up to a given column

4 ビュー (過去 30 日間)
MByk
MByk 2017 年 7 月 12 日
コメント済み: MByk 2017 年 7 月 13 日
How can I find the number of intersecting elements of a matrix up to a user defined column? For example, intersection of first 3 elements. This is what I tried, but it is not working. Thx for the help.
r=[1 2 3 4 5 6; 2 3 4 5 6 1; 1 5 6 3 2 4];
d=3;
dist=pdist(r,@(a,b)length(intersect(a(:,1:d),b(:,1:d))))

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 12 日
[A,B] = ndgrid(1:size(r,1),1:size(r,1));
dist = arrayfun(@(aidx,bidx)numel(intersect(r(aidx,1:d),r(bidx,1:d))), A, B);
  5 件のコメント
Walter Roberson
Walter Roberson 2017 年 7 月 13 日
dist=pdist(r,@(a,b) sum(ismember(b(:,1:d), a(:,1:d)),2))
"A distance function must be of form
d2 = distfun(XI,XJ)
taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:)."
MByk
MByk 2017 年 7 月 13 日
Thank you this one is far too simple to interpret.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by