combination from multiple arrays while omitting same item

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

Are the three arrays always the same?
excellent question. i forgot to mention. the answer is both yes and no. 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)

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
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)
ans = 1.7969e+20
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

okk, i have not thought of that from this perspective. thank you for pointing that out.
then i guess what i can do is, do nchoosek for each of these 4 groups seperately. and then select a few combination results from those 4 nchoosek results based on my criteria and finally combine all of these results and run nchoosek. will this method work and yield a different answer than what i originally wanted to do?
if the generated result is same, then i do not mind working with my data groups seperately.
You could try. As long as the parts are independent you can process them independendently.
nchoosek(20,2)+nchoosek(100,5)+nchoosek(60,5)+nchoosek(25,3)
ans = 80751522
That is certainly more feasible.
but i still do not know how to omit the same element from the lists while using nchoosek. can you help with that?
breakfast = ["apple", "banana", "melon", "grape", "kiwi"]
breakfast = 1×5 string array
"apple" "banana" "melon" "grape" "kiwi"
lunch = ["apple", "banana", "melon", "grape", "kiwi"]
lunch = 1×5 string array
"apple" "banana" "melon" "grape" "kiwi"
dinner = ["apple", "banana", "melon", "grape", "kiwi"]
dinner = 1×5 string array
"apple" "banana" "melon" "grape" "kiwi"
breakfast_choice = breakfast(randi(length(breakfast)))
breakfast_choice = "melon"
lunch_possibilities = setdiff(lunch, breakfast_choice)
lunch_possibilities = 1×4 string array
"apple" "banana" "grape" "kiwi"
lunch_choice = lunch_possibilities(randi(length(lunch_possibilities)))
lunch_choice = "kiwi"
dinner_possibilities = setdiff(dinner, [breakfast_choice, lunch_choice])
dinner_possibilities = 1×3 string array
"apple" "banana" "grape"
dinner_choice = dinner_possibilities(randi(length(dinner_possibilities)))
dinner_choice = "banana"
the solution that you have provided, isn't there the possibility that i might get the same combination more than once. since you are using randi, if i am not mistaken, i think we might get the same combination more than once. (correct me if i am wrong)
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
ans = 4×3 string array
"apple" "banana" "melon" "apple" "banana" "grape" "apple" "melon" "grape" "banana" "melon" "grape"
inds2=perms(1:N) % check all orders
inds2 = 6×3
3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3
for row=1:size(inds,1)
for k=1:size(inds2,1)
selected=breakfast(inds(row,inds2(k,:)))
end
end
selected = 1×3 string array
"melon" "banana" "apple"
selected = 1×3 string array
"melon" "apple" "banana"
selected = 1×3 string array
"banana" "melon" "apple"
selected = 1×3 string array
"banana" "apple" "melon"
selected = 1×3 string array
"apple" "melon" "banana"
selected = 1×3 string array
"apple" "banana" "melon"
selected = 1×3 string array
"grape" "banana" "apple"
selected = 1×3 string array
"grape" "apple" "banana"
selected = 1×3 string array
"banana" "grape" "apple"
selected = 1×3 string array
"banana" "apple" "grape"
selected = 1×3 string array
"apple" "grape" "banana"
selected = 1×3 string array
"apple" "banana" "grape"
selected = 1×3 string array
"grape" "melon" "apple"
selected = 1×3 string array
"grape" "apple" "melon"
selected = 1×3 string array
"melon" "grape" "apple"
selected = 1×3 string array
"melon" "apple" "grape"
selected = 1×3 string array
"apple" "grape" "melon"
selected = 1×3 string array
"apple" "melon" "grape"
selected = 1×3 string array
"grape" "melon" "banana"
selected = 1×3 string array
"grape" "banana" "melon"
selected = 1×3 string array
"melon" "grape" "banana"
selected = 1×3 string array
"melon" "banana" "grape"
selected = 1×3 string array
"banana" "grape" "melon"
selected = 1×3 string array
"banana" "melon" "grape"

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 External Language Interfaces에 대해 자세히 알아보기

질문:

2021년 8월 3일

편집:

Rik
2021년 8월 3일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by