跨多个向量的重叠成员的分数

Fraction of overlapping members across multiple vectors

我有一个单元格 M 有 n 个单元格,每个单元格包含几个唯一的数字,像这样:

{[15 16 21 26 28 145],[2 5 8 9 15],[20 24 27],[10 11 15 8 6 258 74 1],...}

其中一些值出现在 1 个以上的单元格中。我想计算这些单元格中重叠值的比例。例如,对于上面的 4 个单元格,我有 19 个唯一数字,其中 2 个属于超过 1 个单元格:15 和 8。因此,重叠单元格的比例为 2/19 = .105。请注意,M 中的单元格数量可能会有所不同,因此 M 中唯一数字的数量也会有所不同。有没有人对如何有效地执行此操作有任何建议?我已经尝试 horzcat 连接 M 中的单元格然后使用 unique 但没有完全得到我想要的。

使用 hist() 函数的输出在这里很有用。

M = {[15 16 21 26 28 145],[2 5 8 9 15],[20 24 27],[10 11 15 8 6 258 74 1]};
% Bring data to one matrix.
M2 = cell2mat(M);
% Build a histogram from the data with a bin on each unique element. The
% first output of hist is the number of elements in each bin.
a = hist(M2,unique(M2));
% Calculate the overlap by dividing the number of elements that occur more
% than once by the total number of elements.
overlap = sum(a>1)/numel(a);