Generation of a matrix based on a defined order of numbers

조회 수: 1 (최근 30일)
Fayyaz
Fayyaz 2018년 10월 18일
댓글: Fayyaz 2018년 10월 22일
Hi all,
I need your help regarding the generation of a set of numbers.
Suppose, I have two vectors:
v1 = [2 3 4 5 6 7 8 9]
v2 = [3 4 5 6 7]
From v1, I would like to draw five numbers such that these five numbers include only (and any) two values of v1 (always only two numbers in the sequence), i.e.,
2 2 9 9 9
3 3 7 7 7
2 9 9 9 9
.........etc
Then I would like to draw five number from v2, however, the five numbers drawn from v1 are based on only one number that is (always one number in the sequence),
3 3 3 3 3
4 4 4 4 4
........
In the end, the sequences would include 10 numbers where every sequence of the five numbers drawn from v1 are combined with every other sequence of numbers drawn from v2. That is,
2 2 9 9 9 3 3 3 3 3
2 2 9 9 9 4 4 4 4 4
2 2 9 9 9 5 5 5 5 5
. ............
I would like to find out all the combinations that are possible, i.e., starting from
2 2 2 2 3 3 3 3 3 3
2 2 2 2 3 4 4 4 4 4
2 2 2 2 3 5 5 5 5 5
2 2 2 2 3 6 6 6 6 6
2 2 2 2 3 7 7 7 7 7
.........
.......
8 9 9 9 9 7 7 7 7 7
Many thanks.

채택된 답변

Guillaume
Guillaume 2018년 10월 19일
One way to do it:
v1 = [2 3 4 5 6 7 8 9];
v2 = [3 4 5 6 7];
pick1 = nchoosek(v1, 2)';
idx1 = hankel([1 1 1 1 2], [2 2 2 2]) + permute(2*(0:size(pick1, 2)-1), [1 3 2]);
pick1 = reshape(pick1(idx1), 5, [])
pick2 = repmat(v2', 1, 5);
[pick2, pick1] = ndgrid(num2cell(pick2, 2), num2cell(pick1', 2));
result = cell2mat([pick1(:), pick2(:)])

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 10월 18일
I believe this works:
v1 = [2 3 4 5 6 7 8 9]
v2 = [3 4 5 6 7]
% Get first vector of 5 numbers from v1.
twoIndexes = randperm(numel(v1), 2)
twoNumbers = sort(v1(twoIndexes))
index = randi(4)
firstVec = [twoNumbers(1) * ones(1, index), twoNumbers(2) * ones(1, 5 - index)]
% Get second vector of 5 numbers from v2.
secondIndex = randperm(numel(v2), 1)
secondVec = v2(secondIndex) * ones(1, 5)
% Stitch together to get the final vector.
finalVec = [firstVec, secondVec]
  댓글 수: 3
Image Analyst
Image Analyst 2018년 10월 19일
A set of for loops can do it. Can't you figure it out?
What's the use case for this? Why do you want/need to do this unusual thing?
Fayyaz
Fayyaz 2018년 10월 19일
Thanks for the hint. I need every combination so that at the end I get 10 combinations which minimise the determinant of the asymptotic variance-covariance matrix for a utility function

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2018년 10월 19일
편집: Andrei Bobrov 2018년 10월 19일
v1 = [2 3 4 5 6 7 8 9];
v2 = [3 4 5 6 7];
n = numel(v2);
q = nchoosek(v1,2)';
m = reshape(permute(...
q(2 - tril(ones(n-1,n)) + reshape(0:size(q,2)-1,1,1,[])),[2,1,3]),n,[])';
v = v2(:)*ones(1,5);
n2 = size(m,1);
out = [m(kron((1:n2)',ones(n,1)),:), repmat(v,n2,1)];
Added
m = reshape(permute(q( bsxfun(@plus,2 - tril(ones(n-1,n)),...
reshape(0:size(q,2)-1,1,1,[])) ),[2,1,3]),n,[])'; % for MATLAB <= R2016a
  댓글 수: 1
Fayyaz
Fayyaz 2018년 10월 22일
Dear Andrei Bobrov, Many thanks for this. It works perfectly :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by