Unique factorial design or number sequence

1 次查看(过去 30 天)
Hi,
How would I be able to generate a unique sequence by combining [4 6 8 10 12] and [1 2 3], like the ones below:
4 4 4 4 4 1
4 4 4 4 4 2
4 4 4 4 4 3
4 4 4 4 6 1
4 4 4 4 6 2
4 4 4 4 6 3
. . .. . . . .
12 12 12 12 12 1
12 12 12 12 12 2
12 12 12 12 12 3
However, I do not need repetitions, e.g., I need only one sequence 6 8 8 8 8 1 and NOT 8 6 8 8 8 1

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-11-24
编辑:Andrei Bobrov 2017-11-24
v1 = [4 6 8 10 12];
v2 = [1 2 3];
n1 = length(v1);
n2 = length(v2);
ii = flip(fullfact([n2,n1*ones(1,n1)]),2);
ii = ii(all(diff(ii(:,1:end-1),1,2) >= 0,2),:);
out = [v1(ii(:,1:end-1)),v2(ii(:,end))];
or without fullfact:
c = {1:n1,1:n2};
ii = cell(1,numel(v1)+1);
[ii{end:-1:1}] = ndgrid(c{[2,ones(1,n1)]});
ii = cell2mat(cellfun(@(x)x(:),ii,'un',0));
ii = ii(all(diff(ii(:,1:end-1),1,2) >= 0,2),:);
out = [v1(ii(:,1:end-1)),v2(ii(:,end))'];
EDIT
  3 个评论
Fayyaz
Fayyaz 2017-11-24
Many thanks. That is working perfectly :)

请先登录,再进行评论。

更多回答(2 个)

Image Analyst
Image Analyst 2017-11-24
Here's one way:
v1 = [4 6 8 10 12]
v2 = [1 2 3]
n1 = length(v1)
n2 = length(v2)
row = 1;
output = zeros(n1^5*n2, n1+1);
for k1 = 1 : n1
for k2 = 1 : n1
for k3 = 1 : n1
for k4 = 1 : n1
for k5 = 1 : n1
for k6 = 1 : n2
vec = [v1(k1), v1(k2), v1(k3), v1(k4), v1(k5), v2(k6)];
output(row, :) = vec;
row = row + 1;
end
end
end
end
end
end
% Print to command window:
output

mounika
mounika 2017-11-24
You can start with this:
a = [4 6 8 10 12];
b= [1 2 3];
c = ones(1,6); % initialize to ones
t = randi([1,5],1,1); % to randomly pick a number from 'a'
z = randi([1,3],1,1); % to randomly pick a number from 'a'
c = c.*a(t);
c(:,end) = b(z); % assign the end value with any no of b
disp(c);
This gives numbers like: 10 10 10 10 10 2 8 8 8 8 8 3 2 2 2 2 2 1 ...
I didn't really understand the last part of your question ' However, I do not need repetitions, e.g., I need only one sequence 6 8 8 8 8 1 and NOT 8 6 8 8 8 1'
You can change the above code for your specifications.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by