Problems on doing the combination of two variables (columns) in a matrix.

I have to obtain a test statistics from an equation (T), for a thermodynamic process. Fk matrix is made of variables (columns) and constraints (rows). In my code the only changing matrix would be Fk, so I have to obtain the test for all the possible combinations of 2 variables of Fk(15x2) without repeating any.
the combination of the variables should be like this (where this variables are the columns of Fk):
1,2
1,3
1,4
1,5
1,6
2,3
2,4
2,5
2,6
3,4
......
5,6
I attach here the Excel file of the measured values of the variables from 1 to 6 and the matlab code I have done for the combination of one variable.
Does anybody know how should be the code for all the combinations like this?

 Accepted Answer

nchoosek is your friend:
combinations = nchoosek(1:size(Fk, 2), 2);
for combination = combinations'
var1 = Fk(:, combination(1));
var2 = Fk(:, combination(2));
%do something with var1 and var2
end

5 Comments

I have tried to use this on my code and it seems I don't know how to use what you've put. Could you post me here how should be the code? Sorry for my lack of comprehension but I am new on Matlab and programming.
Thank you for your attention Guillaume!
dat=xlsread('C:\Users\p105312\Desktop\example1.xlsx','C5:E10');
y=dat(:,1); v=dat(:,3); % into local variables
clear dat % don't need any longer
Nby=length(y); % Le nombre des variables mesurées from array size
Nbc=4; % Le nombre de contraintes de systéme
Fk = [1 -1 -1 0 0 0;
0 1 0 -1 0 0;
0 0 1 0 -1 0;
0 0 0 -1 -1 1]
r=Fk*y; %here we compute the r matrix (4x1)
CM=diag(v); %covariance matrix
V=Fk*CM*Fk' % We achieve the V matrix
for j=1:Nby,
T = inv(Fk(:,j)'*inv(V)*Fk(:,j))*(Fk(:,j)'*inv(V)*r)^2;
disp(T);
end
OK, I've shortened up your code to only read the data once and build the covariance matrix from the diag command...
Now, I don't follow where you need the two variables for a given case--it appears the whole vector is used for CM and r so what's the individual index have to do with anything?
Thank you dpb, this is a good improvement! I have finally achieved what I wanted so let me put here the matlab code if you want to take a look at it.
You don't really need to calculate TC or C, they're equal to size(combinations, 1) (and you could calculate them easier with nchoosek(Nby, 2) anyway), and you could simplify your final loop with:
combinations = nchoosek(1:size(Fk, 2), 2);
for combination = combinations' %transpose to iterate over combinations
Aex = Fk(:, combination);
T = inv(Aex'*inv(V)*Aex)*(Aex'*inv(V)*r).^2
end
Pretty much what I gave you in the first place.

Sign in to comment.

Asked:

on 3 Sep 2014

Commented:

on 5 Sep 2014

Community Treasure Hunt

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

Start Hunting!