Azzera filtri
Azzera filtri

Generate list of 2 digit combinations without repetition

212 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Cris LaPierre
Cris LaPierre il 30 Nov 2020
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

Più risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by