excluding a small combinations from a combination?

조회 수: 3 (최근 30일)
metin yilmaz
metin yilmaz 2020년 11월 21일
편집: John D'Errico 2020년 11월 21일
Hello. Would you please explain how to exclude combinations including "2,4,6" from combinations of from fourth combination of the number from 1 to 10?
That is I want to exclude "1 2 4 6 " and "3 2 4 6" "2 4 5 6" etc from, a=[1 2 3 4 5 6 7 8 9 10], nchoosek(a,4), the fourth combinations.
Thank you.

답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 11월 21일
편집: Ameer Hamza 2020년 11월 21일
Try this
a=[1 2 3 4 5 6 7 8 9 10];
M = nchoosek(a,4);
C = mat2cell(string(M), size(M,1), ones(size(M,2),1));
C = regexp(strcat(C{:}), '\d*2\d*4\d*6\d*');
idx = cellfun(@isempty, C);
M = M(idx, :)

John D'Errico
John D'Errico 2020년 11월 21일
편집: John D'Errico 2020년 11월 21일
There are not that many combinations that include the subset [2 4 6] out of nchoosek(1:10,4). This means the oversampling will not be too extensive, and therefore you should just generate the entire set, deleting those that include the offending triplet. In my eyes, this is simple, requiting only 2 lines of code.
xyz = nchoosek(1:10,4);
xyz(any(xyz == 2,2) &any(xyz == 4,2) & any(xyz == 6,2),:) = [];
How about my claim the oversampling is not significant?
nchoosek(10,4)
ans = 210
size(xyz)
ans = 1×2
203 4
So I had only to remove 7 offending combinations. Not worth worrying about to do anything more complicated.
  댓글 수: 2
metin yilmaz
metin yilmaz 2020년 11월 21일
The answer is above my level. After creating xyz, I did this operation. Would you please whether you explain it or refer me to a page in Matlab forums or any source including books?
(xyz == 4,2)
(xyz == 4,2)
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
For your this operation
xyz(any(xyz == 2,2) &any(xyz == 4,2) & any(xyz == 6,2),:) = [];
I understand that you choose all the colums, and choose some elements of rows and delete them by [ ].
It is difficult for me to understand how you choose row elements.
Thank you.
John D'Errico
John D'Errico 2020년 11월 21일
편집: John D'Errico 2020년 11월 21일
Why is it above your level? Surely you cannot think what Ameer wrote is simpler?
You are not looking at the code. Think about what I wrote. What you TRIED are arguments to the function ANY. You cannot just use a form like (xyz,2) and expect anything meaningful.
any(xyz == 4,2)
This test finds all rows of xyz that have a 2 in ANY position. The result will be a logical vector. Then I do the same for 4 and 6.
Between those boolean vectors that indicate if a 2, 4, and 6 ere found, I used a logical and (&) to find the occurences where all three numbers are present in any row.
Setting something equal to [] essentially deletes those rows in MATLAB. Essentially, all I did was to delete every row that had all three of 2 and 4 and 6 in the row. There were exactly 7 rows that had that property.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by