Unique factorial design or number sequence

1 vue (au cours des 30 derniers jours)
Fayyaz
Fayyaz le 24 Nov 2017
Commenté : Fayyaz le 24 Nov 2017
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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 24 Nov 2017
Modifié(e) : Andrei Bobrov le 24 Nov 2017
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 commentaires
Andrei Bobrov
Andrei Bobrov le 24 Nov 2017
I'm corrected my answer.
Fayyaz
Fayyaz le 24 Nov 2017
Many thanks. That is working perfectly :)

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 24 Nov 2017
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 le 24 Nov 2017
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.

Catégories

En savoir plus sur Matrices and 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