Get all unique combinations of 2 columns from a table
조회 수: 9 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Stephen23
2022년 1월 19일
V = {'Item1','Item2','Item3','Item4'};
T = table(1, 5, 7 ,10, 'VariableNames',V)
X = nchoosek(1:numel(V),2);
D = diff(T{:,V}(X),1,2);
Z = cell2table([V(X),num2cell(D)])
댓글 수: 0
추가 답변 (1개)
Cris LaPierre
2022년 1월 19일
댓글 수: 3
Cris LaPierre
2022년 1월 19일
I was probably trying too hard to make use of the variablenames. Stephen's approach is simpler by using column numbers instead.
T = table(1, 5, 7 ,10, 'VariableNames', {'Item1' 'Item2' 'Item3' 'Item4'});
C = nchoosek(T.Properties.VariableNames,2)
delta = diff([T{1,{C{:,1}}};T{1,{C{:,2}}}])
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!