在数组中搜索值的组合 [英] Searching an array for combinations of values
问题描述
这应该是一个相当简单的问题,但是我很想听到一些独特的方法来解决这个问题.我正在使用Matlab,但是可行的C#解决方案也可以为我工作.
This should be a fairly simple question to answer, but I am interested to hear some unique ways to do it, so here goes. I am using Matlab, but a viable C# solution would work for me as well.
我有一个数组(23000行x 3列).每行是一组90个值中的3个值的组合.我想从这90个值中选择一个子集(例如10),并找到这10个值中的任何3个是成员的行,然后返回行号.
I have an array (23000 rows x 3 columns). Each row is a combination of 3 values from a set of 90 values. I would like select a subset of these 90 values, say 10, and find the rows in which ANY 3 of these 10 values are members, and return the row number.
现在,我可以生成这10个值的所有3值组合的列表,然后在Matlab中使用ismember
查找每个组合的行.但是,有其他方法还是更优雅的方法呢?
Now, I could generate a list of all the 3-value combinations of those 10 values, and then use ismember
in Matlab to find the row for each combination. But is there a different or more elegant way?
或者,我可以使用a=sum(ismember(array, 'value'),2)
生成逻辑向量,其中'value'
出现在array
中,并使用b=find(a)
查找出现'value'
的行索引.我可以为10的每个值执行此操作,但是现在问题出在这10个索引列表中,哪个索引出现3次或更多次?
Alternately, I could use a=sum(ismember(array, 'value'),2)
to generate a logical vector where 'value'
occurs in array
, and use b=find(a)
to find the row indices where 'value'
occurs. I could do this for each value of the 10. But now the problem becomes, of these 10 lists of indices, which index occurs 3 or more times?
任何想法/评论/问题都值得赞赏.谢谢!
Any thoughts/comments/questions are appreciated. Thanks!
推荐答案
您对逻辑索引的建议几乎是正确的.
You were almost right with your suggestion of logical indexing.
使向量具有所有可能的值:
Make value a vector with all the possible values:
value = [1 2 3 4 5 6 7 8 9 10];
现在您可以一次在所有值上使用ismember.
Now you can use ismember on all the values at once.
logical_array = ismember(array, value);
num_matches = sum(logical_array,2);
rows_with_3_matches = find(num_matches==3);
logical_vector = num_matches==3;
这篇关于在数组中搜索值的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!