Unique factorial design or number sequence

1 回表示 (過去 30 日間)
Fayyaz
Fayyaz 2017 年 11 月 24 日
コメント済み: Fayyaz 2017 年 11 月 24 日
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 件のコメント
Andrei Bobrov
Andrei Bobrov 2017 年 11 月 24 日
I'm corrected my answer.
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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by