combination from multiple arrays while omitting same item
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
so i have multiple arrays and each of these array will have the same data set. now i want to find all combinations of items from these arrays but as all of them will have the same items, i want to omit the item which is selected from the first array.
example- which fruits to eat in a day.
breakfast= ['apple', 'banana', 'melon', 'grape', 'kiwi']
lunch= ['apple', 'banana', 'melon', 'grape', 'kiwi']
dinner= ['apple', 'banana', 'melon', 'grape', 'kiwi']
result= when 'apple' is selected from the breakfast list then 'apple' should not be selected from lunch and dinner lists.
Please also note, that the array sizes will be both equal and unequal. Meaning in my original work, i will have 2 arrays with 20 elements, 5 arrays with 100 elements, 5 arrays with 60 elements and 3 arrays with 25 elements. and finally i want to do combination of all of these 15 arrays together. where the condition of omission applies to the groups of arrays which have similar number of elements (basically i have 4 groups of data, and among these 4 groups one group of data will be put in to 2 arrays, another group will be put into 5 arrays, another group will be put into 5 arrays and the last group will be put into 3 arrays)
i may use allcomb from File Exchange or some other codes but i need to know how to omit the same elements from the following lists while running the code.
thanks.
2 件のコメント
回答 (1 件)
Rik
2021 年 8 月 3 日
We have a small problem when we want to generate all combinations:
- C = nchoosek(v,k) is only practical for situations where length(v) is less than about 15.
But there is a more fundamental problem:
nchoosek(20,2)*nchoosek(100,5)*nchoosek(60,5)*nchoosek(25,3)
The current age of the universe is 4e17 seconds, so you need extremely fast processing of what you want to do, otherwise this will not finish before you die of old age.
6 件のコメント
Rik
2021 年 8 月 3 日
編集済み: Rik
2021 年 8 月 3 日
Since Walter's code uses setdiff to remove the already picked element, it is impossible to get the same element twice.
For the cases where your options vector is small enough you can this to generate the indices. This quickly grows out of hand, so you will soon need to use a strategy like the one Walter suggested.
breakfast = ["apple", "banana", "melon", "grape"];
N=3;
inds=nchoosek(1:numel(breakfast),N);
breakfast(inds) % contains only sorted combination
inds2=perms(1:N) % check all orders
for row=1:size(inds,1)
for k=1:size(inds2,1)
selected=breakfast(inds(row,inds2(k,:)))
end
end
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!