Generate list of 2 digit combinations without repetition

221 ビュー (過去 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 件のコメント
Aäron Penders
Aäron Penders 2020 年 12 月 1 日
Great answer.Thanks for your help!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by