Combination of numbers with specific order

2 次查看(过去 30 天)
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 个评论
Fayyaz
Fayyaz 2017-10-13
编辑:Walter Roberson 2017-10-13
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 2017-10-13
Probably a couple of for loops is the easiest way to handle it.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-10-13
编辑:Andrei Bobrov 2017-10-13
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 个评论
Fayyaz
Fayyaz 2017-10-13
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-10-13
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 个评论
Fayyaz
Fayyaz 2017-10-13
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!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by