1

I'm newbie in MATLAB. I get confused to compare all possible combination column in matrix. Assume that I have an input data as a matrix 100x4. I want to compare all columns with corrcoef function (built in matlab) and save the result. So, the total combination will N*(N-1)/2.

clc
clear all
close all

load input;
% data will be a matrix with 100x4
[N,nu] = size(input);
R1_2 = corrcoef(input(:,1),input(:,2));
R1_3 = corrcoef(input(:,1),input(:,3));
R1_4 = corrcoef(input(:,1),input(:,4));
R2_3 = corrcoef(input(:,2),input(:,3));
R2_4 = corrcoef(input(:,2),input(:,4));
R3_4 = corrcoef(input(:,3),input(:,4));

I think it can be solved by 'for loop'. Anybody can help how to solve this problem? Thank you

marsei
  • 7,691
  • 3
  • 32
  • 41
  • solve it with or without loop? – Divakar Jan 11 '16 at 08:31
  • Will you matrix always have 4 columns? If so it's totally acceptable to hard code it as it only takes 6 lines. Otherwise you may want to take a look at `nchoosek` – BillBokeey Jan 11 '16 at 08:32
  • Use the duplicate post to generate all unique combinations of pair indices, then loop over each pair, index into the matrix to pull out these two columns and apply `corrcoef`. You could probably do this vectorized but start simple and do this first to get it working. – rayryeng Jan 11 '16 at 09:04
  • @rayryeng Question closed too soon. The correct answer in this case is that `[R,P] = corrcoef(A)` where A is a `100x4` matrix will give a `4x4` correlation matrix. In other words, the column combination is already included in the `corrcoef` function. – marsei Jan 11 '16 at 09:26
  • 1
    @marsei allow me to reopen so you can write an answer. – rayryeng Jan 11 '16 at 14:31
  • @Divakar, yes using loop. Actually my data contain 3000x300. Thank you for your help – nafilatur2724 Jan 12 '16 at 07:20
  • @BillBokeey No, my matrix is 3000x300 actually. Thank you for your help – nafilatur2724 Jan 12 '16 at 07:20

1 Answers1

1

[R,P] = corrcoef(A) where A is a 100x4 matrix will give a 4x4 correlation matrix.

In other words, the column combinations are already included in the corrcoef function. You can access the paired comparisons with regular indexing:

R(3,4) for the (3rd,4th) column coefficient.

marsei
  • 7,691
  • 3
  • 32
  • 41