In MATLAB, how can I calculate the unique number of index values ​​associated with a condition?

I have a two-dimensional matrix containing the index of the experimental condition in the first column, and the index of the corresponding experiment in the second column, i.e. [condition experiment]. Each line corresponds to one interesting event (one experiment can produce one or more events).

Counting conditions and events are easy. I would like to know how to calculate how many unique experiments there were for each given condition.

This is the solution that I have right now using ACCUMARRAY , but I think there should be a simpler or more elegant solution:

idxList = [1 1;...  %# There are two experiments for condition 1...
           1 2;...
           1 2;...
           2 1;...  %# ...and 1 experiment for condition 2.
           2 1];
accumarray(idxList(:,1),idxList(:,2),[],@(x)length(unique(x)))
ans =
     2
     1
+3
1

:

  • idxList subs ACCUMARRAY (.. ), :

    experCounts = sum(accumarray(idxList,1) > 0,2);
    
  • UNIQUE idxList , , ACCUMARRAY:

    idxList = unique(idxList,'rows');
    experCounts = accumarray(idxList(:,1),1);
    
+2

Source: https://habr.com/ru/post/1778884/


All Articles