필터 지우기
필터 지우기

Generate list of 2 digit combinations without repetition

조회 수: 209 (최근 30일)
Aäron Penders
Aäron Penders 2020년 11월 30일
댓글: Aäron Penders 2020년 12월 1일
I'm trying to generate a list of unique 2 digit number combinations from 0 to 9 without repetition. Using npermutek:
a = npermutek(0:9,2)
% OUTPUT:
% a =
%
% 0 0
% 0 1
% 0 2
% 0 3
% 0 4
% 0 5
% 0 6
% 0 7
% 0 8
% 0 9
% 1 0 <-- Repetitive
% 1 1
% 1 2
% 1 3
% 1 4
% 1 5
% 1 6
% 1 7
% 1 8
% 1 9
% 2 0
% 2 1 <-- Repetitive
% ... ...
% 9 9
However I want to filter out the repetitive number combinations like e.g. 10 and 21, since they fall into the same category as 01 and 12 respectively. nchoosek(0:9,2) does not suit my needs as numbers like 00, 11 ... are not represented.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 11월 30일
I suspect [2 0] meets your standard for being repetitive as well? I'm assuming yes, otherwise the definition seems arbitrary.
If so, the trick is to notice that any value in the second column that is less than the value in the first column would create a repeat value. Using nested for loops, you could use the value of the outer loop to set the starting point for the inner loop.
C=[];
for a = 0:9
for b = a:9
C=[C;a b];
end
end
% visualize the bottom 10 rows of the array
C(end-9:end,:)
ans = 10×2
6 6 6 7 6 8 6 9 7 7 7 8 7 9 8 8 8 9 9 9

추가 답변 (1개)

James Tursa
James Tursa 2020년 11월 30일
Can't you just use the nchoosek(0:9,2) result and add in the known double results 00, 11, etc.?
  댓글 수: 1
Aäron Penders
Aäron Penders 2020년 12월 1일
Thanks for your reply James. That would have worked, but a direct solution is more elegant.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by