Clear Filters
Clear Filters

Combination of numbers with specific order

3 views (last 30 days)
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 Comments
Fayyaz
Fayyaz on 13 Oct 2017
Edited: Walter Roberson on 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 on 13 Oct 2017
Probably a couple of for loops is the easiest way to handle it.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 13 Oct 2017
Edited: Andrei Bobrov on 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 Comments
Fayyaz
Fayyaz on 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 on 13 Oct 2017
See my answer: part after word "or".

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 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 Comment
Fayyaz
Fayyaz on 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!!

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!