Search code examples
matlabvectorneural-networkmembership

Count the number of membership changes across multiple vectors


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

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.

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.

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?

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

    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).

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.


Solution

  • 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

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