Effacer les filtres
Effacer les filtres

Combination of numbers with specific order

3 vues (au cours des 30 derniers jours)
Fayyaz
Fayyaz le 13 Oct 2017
Commenté : Fayyaz le 13 Oct 2017
Hi all,
I would like to create a matrix of combination of numbers (with five columns). The number are: 4,6,8,10,12,14
The matrix should be like this:
4 4 4 4 4
4 4 4 4 6
4 4 4 6 6
.
.
.
.
14 14 14 14 14
Please help me out!
  3 commentaires
Fayyaz
Fayyaz le 13 Oct 2017
Modifié(e) : Walter Roberson le 13 Oct 2017
Many thanks for the comment. Your comment made me think about it. I was a bit vague. Please see the attached.
4 4 4 4 4
4 4 4 4 6
4 4 4 6 6
4 4 6 6 6
4 6 6 6 6
4 4 4 4 8
4 4 4 8 8
4 4 8 8 8
4 8 8 8 8
4 4 4 4 10
4 4 4 10 10
4 4 10 10 10
4 10 10 10 10
4 4 4 4 12
4 4 4 12 12
4 4 12 12 12
4 12 12 12 12
4 4 4 4 14
4 4 4 14 14
4 4 14 14 14
4 14 14 14 14
6 6 6 6 8
6 6 6 8 8
6 6 8 8 8
6 8 8 8 8
6 6 6 6 10
Walter Roberson
Walter Roberson le 13 Oct 2017
Probably a couple of for loops is the easiest way to handle it.

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 13 Oct 2017
Modifié(e) : Andrei Bobrov le 13 Oct 2017
out = nchoosek(kron(4:2:14,ones(1,5)),5);
or
x = kron(4:2:14,ones(1,5));
out = hankel(x(1:end-4),x(end-4:end));
  2 commentaires
Fayyaz
Fayyaz le 13 Oct 2017
Dear Andrei, many thanks.
It works but there is a small problem. I got this matrix (with a number of repetitions):
4 4 4 4 4
4 4 4 4 6
4 4 4 4 6
4 4 4 4 6
4 4 4 4 6
4 4 4 4 6
4 4 4 4 8
4 4 4 4 8
4 4 4 4 8
4 4 4 4 8
4 4 4 4 8
Any recommendation how would I be able to get a unique sequence like:
4 4 4 4 4
4 4 4 4 6
4 4 4 6 6
Thanks in advance.
Andrei Bobrov
Andrei Bobrov le 13 Oct 2017
See my answer: part after word "or".

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 13 Oct 2017
Slightly vectorized:
V = 4 : 2 : 14;
num_V = length(V);
pattern = [1 1 1 1 2;
1 1 1 2 2;
1 1 2 2 2;
1 2 2 2 2];
num_pattern = size(pattern,1);
nrow = num_pattern * (num_V - 1) + 1;
out = zeros(nrow, 5);
out(1, :) = V(1);
for idx = 2 : num_V
pair = [V(1), V(idx)];
this_set = pair(pattern);
start = 1 + (idx-2) * num_pattern;
out(start + 1 : start + num_pattern, :) = this_set;
end
  1 commentaire
Fayyaz
Fayyaz le 13 Oct 2017
Many thanks. It works perfectly. I have already written manually since it was not that long. However, for my next enumeration, I will keep this handy!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by