Filter löschen
Filter löschen

Combination of numbers with specific order

3 Ansichten (letzte 30 Tage)
Fayyaz
Fayyaz am 13 Okt. 2017
Kommentiert: Fayyaz am 13 Okt. 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 Kommentare
Fayyaz
Fayyaz am 13 Okt. 2017
Bearbeitet: Walter Roberson am 13 Okt. 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 am 13 Okt. 2017
Probably a couple of for loops is the easiest way to handle it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 13 Okt. 2017
Bearbeitet: Andrei Bobrov am 13 Okt. 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 Kommentare
Fayyaz
Fayyaz am 13 Okt. 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 am 13 Okt. 2017
See my answer: part after word "or".

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 13 Okt. 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 Kommentar
Fayyaz
Fayyaz am 13 Okt. 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!!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by