Generate list of 2 digit combinations without repetition

212 次查看(过去 30 天)
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.?

类别

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