# Get all unique combinations of 2 columns from a table

3 views (last 30 days)
Austin Burritt on 19 Jan 2022
Commented: Austin Burritt on 19 Jan 2022
I have a table that has 4 variables, similar to this:
Item1 Item2 Item3 Item4
_____ _____ _____ _____
1 5 7 10
What is the best way to produce the delta of each combination of columns? Ex. Item1 vs Item2, Item1 vs Item3, etc.

Stephen23 on 19 Jan 2022
V = {'Item1','Item2','Item3','Item4'};
T = table(1, 5, 7 ,10, 'VariableNames',V)
T = 1×4 table
Item1 Item2 Item3 Item4 _____ _____ _____ _____ 1 5 7 10
X = nchoosek(1:numel(V),2);
D = diff(T{:,V}(X),1,2);
Z = cell2table([V(X),num2cell(D)])
Z = 6×3 table
Var1 Var2 Var3 _________ _________ ____ {'Item1'} {'Item2'} 4 {'Item1'} {'Item3'} 6 {'Item1'} {'Item4'} 9 {'Item2'} {'Item3'} 2 {'Item2'} {'Item4'} 5 {'Item3'} {'Item4'} 3

Cris LaPierre on 19 Jan 2022
I would look into using the perms function, along with the unique and diff functions.
Austin Burritt on 19 Jan 2022
His and yours are both excellent and elegant, thank you!

R2021b

### Community Treasure Hunt

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

Start Hunting!