在 MATLAB 中,如何计算与条件关联的索引值的唯一数量?

发布于 2024-10-06 08:33:28 字数 576 浏览 0 评论 0原文

我有一个二维矩阵,其中第一列包含实验条件的索引,第二列包含相应实验的索引,即[条件实验]。每一行对应一个有趣的事件(一个实验可以产生一个或多个事件)。

计算条件和事件很容易。我想知道如何计算每个给定条件下有多少个独特的实验。

这是我现在使用 ACCUMARRAY 的解决方案,但我认为有应该是一个更简单或更优雅的解决方案:

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

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

Counting conditions and events is easy. I'd like to know how to count how many unique experiments there were for each given condition.

This is the solution 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-10-13 08:33:28

这里有几个替代方案:

  • 使用整个矩阵idxList作为ACCUMARRAY(即指定累积的行索引和列索引),然后对结果行中的非零数求和:

    experCounts = sum(accumarray(idxList,1) > 0,2);
    
  • 使用首先在 idxList 上使用 UNIQUE 删除重复行,这简化了对 ACCUMARRAY 的调用:

    idxList = unique(idxList,'行');
    experCounts = AccumArray(idxList(:,1),1);
    

Here are a couple of alternatives:

  • Use the entire matrix idxList for the subs argument to ACCUMARRAY (i.e. specify both row and column indices for the accumulation), then sum the number of non-zeroes across the rows of the result:

    experCounts = sum(accumarray(idxList,1) > 0,2);
    
  • Use UNIQUE on idxList first to remove duplicate rows, which simplifies the call to ACCUMARRAY:

    idxList = unique(idxList,'rows');
    experCounts = accumarray(idxList(:,1),1);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文