combination from multiple arrays while omitting same item

조회 수: 3 (최근 30일)
Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2021년 8월 3일
편집: Rik 2021년 8월 3일
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
Rik
Rik 2021년 8월 3일
Are the three arrays always the same?
Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2021년 8월 3일
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
Muhammad Ridwanur Rahim
Muhammad Ridwanur Rahim 2021년 8월 3일
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)
Rik
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
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"

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

Community Treasure Hunt

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

Start Hunting!

Translated by