How do I correlate columns

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

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

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)

How's this?
m = rand(2400,1015);
c = corrcoef(m(:,[1 926:1015]));
imagesc(c)
cb = colorbar;
ylabel(cb,'correlation coefficient')

4 Comments

Thanks!
I don't think the rand function works for my data. I have provided a picture to help me explain. E.g. I would like to correlate every value in column 1 with every value in column 5, then every value in column 1 with every value on column 6 and so on, until I have correlated column 1 with columns 926 through 1015.
After column 1 has been correlated with columns 926 through 1015, I would like column 2 to do the same thing (correlate with columns 926 through 1015). So eventually columns 1 through 925 have correlated themselves with columns 926 though 1015.
Also, is there a way of averaging each correlation so that each correlation only produces 1 average number. E.g. column 1 correlates with column 5 and produces 1 number.
My end goal is to have a 925 x 90 correlation matrix.
Hello, Kris,
What operation are intending to indicate by the word "correlate"? I may be unfamiliar with the application, and thereby having trouble understanding.
Best,
Jonathan Campelli
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.
Alright, cooking up some code...

Sign in to comment.

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

Community Treasure Hunt

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

Start Hunting!