All combinations of a data set without mixing data across arrays
1 view (last 30 days)
Show older comments
Given the following data
Var1 = [1400, 1700, 1900]
Var2 = [1340, 1680, 1900, 1871, 789]
Var3 = [900]
Var4 = [950, 680]
Var5 = [100, 150, 150, 900]
I want to get all of the unique combinations of data without mixing between data sets i.e.
1400 1340 900 950 100
I am pretty new to MATLAB and am looking for a quick easy way to do this.
0 Comments
Answers (2)
Walter Roberson
on 8 Aug 2023
Var1 = [1400, 1700, 1900];
Var2 = [1340, 1680, 1900, 1871, 789];
Var3 = [900];
Var4 = [950, 680];
Var5 = [100, 150, 150, 900];
[temp1{1:5}] = ndgrid(Var1, Var2, Var3, Var4, Var5);
temp2 = cellfun(@(m) m(:), temp1, 'Uniform', 0);
output = [temp2{:}];
size(output)
disp(output(1:15,:))
You can see from the excerpt that output is working its way through all combinations, with the first column varying most quickly.
0 Comments
Bruno Luong
on 8 Aug 2023
Edited: Bruno Luong
on 9 Aug 2023
Var1 = [1400, 1700, 1900];
Var2 = [1340, 1680, 1900, 1871, 789];
Var3 = [900];
Var4 = [950, 680];
Var5 = [100, 150, 150, 900];
C = combinations(Var1, Var2, Var3, Var4, Var5)
% if prefered array output instead of table
A = table2array(C)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!