Unique factorial design or number sequence

1 Ansicht (letzte 30 Tage)
Fayyaz
Fayyaz am 24 Nov. 2017
Kommentiert: Fayyaz am 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

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 24 Nov. 2017
Bearbeitet: Andrei Bobrov am 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 Kommentare
Andrei Bobrov
Andrei Bobrov am 24 Nov. 2017
I'm corrected my answer.
Fayyaz
Fayyaz am 24 Nov. 2017
Many thanks. That is working perfectly :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 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 am 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.

Kategorien

Mehr zu Matrices and 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