How do I correlate columns

20 views (last 30 days)
Kris
Kris on 13 Apr 2015
Answered: Jonathan Campelli on 14 Apr 2015
I have 2400 x 1015 matrix. I would like to correlate column 1 with column 926. I would then like to correlate column 1 with 927 and so on until I correlate column 1 with column 1015.
I would then like to print the result of each correlation in row 1, in a new file.
Thanks
  1 Comment
Jonathan Campelli
Jonathan Campelli on 14 Apr 2015
I've made a program that's computing each correlation by the formula, which is returning totally different results from my previous code. Just updating you that I'll post the correct code within the next couple hours, in case you have need of it still. "corrcoeff" was yielding different results than those you asked for. Coming soon...
Jonathan Campelli

Sign in to comment.

Accepted Answer

Jonathan Campelli
Jonathan Campelli on 14 Apr 2015
Hello again, Kris,
This function outputs a 925x90 matrix with individually computed linear correlation coefficients for each of the comparisons you described. It took me a short while to determine precisely what you were seeking to compute and what format you intended:
function [ output_args ] = KrisCorrelation( matrix )
n=size(matrix,1); %Determine number of rows with "size" and"1"
standard=zscore(matrix); %Standardize every column in matrix with "zscore"
x=1:925; %Set first range of columns for correlation
y=926:1015; %Set second range of columns for correlation
%%Use matrix multiplication to multiply column 1 by columns 926:1015, then
%%column 2 by columns 926:1015, and so on until all multiplications are
%%complete. Matrix multiplication sums the values in every column
%%automatically, and this time it leaves one row vector of length 90 for
%%each of the 925 separate loops. This equation completes the equations
%%for finding the correlation coefficient:
coefficients=(standard(:,x)'*standard(:,y))/(n-1);
%Save correlation variable to MAT file:
save('KrisCoefficients', 'coefficients')
%Save correlation variable "correlationResults" to XLSX file:
xlswrite('KrisCoefficients.xlsx', coefficients)
end
Once you hit "Accept Answer" on the solution that meets your needs, we'll know your all set.
Best regards,
Jonathan Campelli

More Answers (2)

Chad Greene
Chad Greene on 13 Apr 2015
How's this?
m = rand(2400,1015);
c = corrcoef(m(:,[1 926:1015]));
imagesc(c)
cb = colorbar;
ylabel(cb,'correlation coefficient')
  4 Comments
Kris
Kris on 13 Apr 2015
I want to know the strength of the relationship between the values in column 1 compared with the values in columns 926, I then want to know the strength of the relationship of the values in column 1 compared with values in column 927 and so on until I reach column 1015. I then want to repeat this operation for columns 1 - 925.
Jonathan Campelli
Jonathan Campelli on 13 Apr 2015
Alright, cooking up some code...

Sign in to comment.


Chad Greene
Chad Greene on 13 Apr 2015
The rand function was only to generate random data to play with.
I don't see how you'll get a 925 x 90 correlation matrix. Here I'll make up some data, but set column 1 to sine wave, then columns 926 to the end will have an increasingly high sine wave to random noise ratio. So correlation increases from columns 926 to 1015, as is evident in the top row of subplot 3:
% Some random data:
m = rand(2400,1015);
% The first column is sine function:
m(:,1) = sind(1:2400);
% Force correlation to come increasingly positive
% from cols 926 to 1015,
mag = 0;
for k = 926:1015
m(:,k) = m(:,k) + mag*sind(1:2400)';
mag = mag+.01;
end
subplot(131)
imagesc(m)
title 'full dataset'
subplot(132)
imagesc(corrcoef(m))
title 'correlation all data'
caxis([-1 1])
subplot(133)
c = corrcoef(m(:,[1 926:1015]));
imagesc(c)
title 'correlation of cols 1, 926:1015'
caxis([-1 1])
rgbmap('red','white','blue')

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!