Combine Vectors Into Matrix - NOT ALL Possible Combinations from baseline
1 view (last 30 days)
Show older comments
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
Mark Sherstan
on 10 Feb 2019
Have you considered using the built in DOE functions? fullfact and fracfactgen might be useful. More info here: https://www.mathworks.com/help/stats/design-of-experiments-1.html
Accepted Answer
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
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!
More Answers (0)
See Also
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!