在 MATLAB 中,如何计算与条件关联的索引值的唯一数量?
我有一个二维矩阵,其中第一列包含实验条件的索引,第二列包含相应实验的索引,即[条件实验]
。每一行对应一个有趣的事件(一个实验可以产生一个或多个事件)。
计算条件和事件很容易。我想知道如何计算每个给定条件下有多少个独特的实验。
这是我现在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个替代方案:
使用整个矩阵
idxList
作为ACCUMARRAY(即指定累积的行索引和列索引),然后对结果行中的非零数求和:使用首先在
idxList
上使用 UNIQUE 删除重复行,这简化了对 ACCUMARRAY 的调用:Here are a couple of alternatives:
Use the entire matrix
idxList
for thesubs
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:Use UNIQUE on
idxList
first to remove duplicate rows, which simplifies the call to ACCUMARRAY: