Searching an array for combinations of values

2019-06-13 16:02发布

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.

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.

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?

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!

1条回答
Bombasti
2楼-- · 2019-06-13 16:07

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];

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;
查看更多
登录 后发表回答