Combine Vectors Into Matrix - NOT ALL Possible Combinations from baseline

1 view (last 30 days)
Hello All,
I have come across ways of combining vectors into a single matrix with all possbile matrices, however I am looking to step through changes for a DOE for example, so order matters and I only want to change one parameter at a time.
So if I have
A = {1, 2, 3, 4};
B = {7, 8, 9};
C = {10, 11, 12, 13};
D = {14, 15, 16};
It would yeild
Consider { 1 7 10 14} as a "baseline" and then the full DOE would look like...
The number of vectors would be dynamic and the length of the vectors as well.
Combs = 1 7 10 14
2 7 10 14
3 7 10 14
4 7 10 14
1 8 10 14
1 9 10 14
1 7 11 14
1 7 12 14
1 7 13 14
1 7 10 15
1 7 10 16
Any syntax help is greatly apperciated!
  2 Comments
AJ Foyt
AJ Foyt on 10 Feb 2019
Thank you Mark, That is an interesting toolbox I was not aware of. Unfortunately I do not have the licensing for that toolbox.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 11 Feb 2019
Edited: Guillaume on 11 Feb 2019
allpoints = {[1 2 3 4], [7 8 9], [10 11 12 13], [14 15 16]}; %can be as many vectors of any length
baseline = [1 7 10 14];
assert(isequal(size(baseline), size(allpoints)), 'Shape and size of baseline doesn''t match allpoints');
assert(all(cellfun(@ismember, num2cell(baseline), allpoints)), 'Some points in baseline are not in allpoints');
result = cell(size(allpoints));
for vecidx = 1:numel(allpoints) %iterate over the test vectors
testmatrix = repmat(baseline, numel(allpoints{vecidx}), 1); %replicate baseline to number of elements in current vector
testmatrix(:, vecidx) = allpoints{vecidx}(:); %and replace test column by all the points in that vector
result{vecidx} = testmatrix;
end
result = vertcat(result{:}) %concatenate the whole lot
Generates only the required combinations so should be a lot faster than generating all combinations and then discarding the unwanted ones.
  2 Comments
Stephan
Stephan on 11 Feb 2019
Just recognized that my approach would fail in cases where the content of allpoints is repeated in some entries. Nice solution!

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!