特定的多集比较

specific multisets comparison

我正在寻找矢量化 matlab 函数来解决以下问题:

我已经对多重集进行了排序T = [1 1 1 1 2 2 2 3]

T 的排序子多重集 Vlength(V) 总是小于 length(T)

V = [ 1 1 1 2]

我需要找到逻辑向量

D = [1 1 1 0 1 0 0 0]

其中 length(D) = length(T)T(D) = V

为了获得良好的性能,我的想法是使用直方图,因此数据量较小:

T = [1 1 1 1 2 2 2 3];
V = [ 1 1 1 2];
%Get list of all symbols
E=unique(T);
hT=hist(T,E);
hV=hist(V,E);
rep=[hV;hT-hV];
%Next two lines are taken from this answer 
R=mod(cumsum(accumarray(cumsum([1; rep(:)]), 1)),2);
R=R(1:end-1);

可以使用 matlab 函数 repelem 而不是最后两行:

R=mod(repelem(1:numel(rep),rep(:)),2);