计算多个向量的成员资格变化数

时间:2021-07-27 19:38:34

I have 2 vectors in which elements of a similar value are considered to be of the same group, something like this:

我有2个向量,其中类似值的元素被认为是同一组,如下所示:

V1    V2

1     7
1     8
1     8
1     8
1     9
2     10
3     11
3     11
3     11
3     12
4     12
4     12

In this example, V1 has 4 groups, group 1 has the first 5 elements , group 2 has the next 1 element, group 3 has the next 4 elements, and group 4 has the last 2 elements. V2 has 5 groups, group 1 has the first element, group 2 has the next 3 elements, etc.

在此示例中,V1具有4个组,组1具有前5个元素,组2具有接下来的1个元素,组3具有接下来的4个元素,组4具有最后2个元素。 V2有5组,组1有第一个元素,组2有下3个元素,等等。

Now, I would like to count the number of time an element switches groups, using V1 as the reference. Let's consider group 1 in V1. The first 5 elements are in this same group. In V2, that's no longer the case because V2(1,1) and V2(5,1) do not have the same value as the remaining elements and thus are considered to have switched/changed membership. Applied the same principle, there is no switch for group 2 (i.e.,V1(6,1) and V2(6,1)), one switch for group 3, and no switch for group 4. Total is 3 switches.

现在,我想计算一个元素切换组的时间,使用V1作为参考。让我们考虑V1中的第1组。前5个元素属于同一组。在V2中,情况不再是这种情况,因为V2(1,1)和V2(5,1)与剩余元素的值不同,因此被认为已经切换/改变了成员资格。应用相同的原理,第2组没有开关(即V1(6,1)和V2(6,1)),第3组有一个开关,第4组没有开关。总共有3个开关。

At first I thought this would be a simple calculation with no. of switches = numel(unique(V1)) - numel(unique(V2)). However, as you can see, this underestimates the number of switches. Does anyone have a solution to this?

起初我认为这将是一个简单的计算,没有。 of switches = numel(unique(V1)) - numel(unique(V2))。但是,正如您所看到的,这低估了开关的数量。有人有解决方案吗?

I also welcome a solution to a simpler problem in which V1 contains only one group, like this:

我也欢迎解决一个更简单的问题,其中V1只包含一个组,如下所示:

    V1    V2

    2     7
    2     8
    2     8
    2     8
    2     8
    2     8
    2     8
    2     9
    2     8
    2     10
    2     10
    2     8

In this second case, the count is 4 nodes that switch: V2(1,1), V2(8,1), V2(10,1), V2(11,1).

在第二种情况下,计数是4个节点,它们切换:V2(1,1),V2(8,1),V2(10,1),V2(11,1)。

Side note: this is actually a network problem: V1 and V2 are partitions and I'm trying to count the number of time a node switches membership.

旁注:这实际上是一个网络问题:V1和V2是分区,我正在尝试计算节点切换成员资格的时间。

1 个解决方案

#1


2  

Here is a solution using unique and accumarray

这是一个使用独特和准确的解决方案

    u = unique([V1 V2],'rows');
    switches  = accumarray(u(:,1) , 1, [],@numel)-1;
    total_switches = sum(switches)

or you can use histcounts

或者您可以使用histcounts

    u = unique([V1 V2],'rows');
    switches  = histcounts(u(:,1) , [unique(u(:,1)); u(end,1)])-1;
    total_switches = sum(switches)

#1


2  

Here is a solution using unique and accumarray

这是一个使用独特和准确的解决方案

    u = unique([V1 V2],'rows');
    switches  = accumarray(u(:,1) , 1, [],@numel)-1;
    total_switches = sum(switches)

or you can use histcounts

或者您可以使用histcounts

    u = unique([V1 V2],'rows');
    switches  = histcounts(u(:,1) , [unique(u(:,1)); u(end,1)])-1;
    total_switches = sum(switches)