# How do I correlate elements from two different cell array files?

3 views (last 30 days)
lil brain on 14 Feb 2022
Commented: lil brain on 15 Feb 2022
Dear community,
They both look like this (in reality they are much larger with many more cells):
19×1 cell array
{512×54 double}
{512×50 double}
...
19×1 cell array
{512×53 double}
{512×49 double}
...
Inside of each cell is a matrix with columns that are all 512 elements in length. What I want to do is correlate each of these columns with the matching column in the other file and save it in a cell array file called "correlations".
Example:
corr( X1 , Y1 )
corr( X2 , Y2 )
... and so on.
Basically, I want to do that until all the columns in the cells of "distances_head_lefthand_windows" are correlated with all the columns of the cells in "distances_head_righthand_windows".
How would I do this?
(Sorry if this is overly complicated but I hope it makes sense. And sorry for the long names)
##### 2 CommentsShow 1 older commentHide 1 older comment
lil brain on 15 Feb 2022
Hi @the cyclist thanks for the response!
1) Yes I would like to ingnore the last column. And yes, the "left" matrix is always one column bigger.
2) And yes, the output, using that example, should ideally be a 1x53 vector with 53 columns. in correlations each cell represents a participant. And in that case each participant should ideally have a 1xn vector of coefficients.
Thanks!

the cyclist on 15 Feb 2022
This is done straightforwardly with loops. If this is fast enough, I would not bother trying to optimize for speed, since it is better to understand what the code is doing.
L = {rand(4,3); rand(5,7)};
R = {rand(4,2); rand(5,6)};
% Preallocate memory for output
correlations = cell(size(L));
% Loop over each cell
for ncell = 1:size(L,1)
% Define the number of correlation coefficients in this cell
ncorr = size(R{ncell},2);
% Preallocate the correlation vector
correlations{ncell} = zeros(1,ncorr);
% Loop over each pair of columns
for ncoeff = 1:ncorr
% Calculate the correlation coefficient
correlations{ncell}(ncoeff) = corr(L{ncell}(:,ncoeff),R{ncell}(:,ncoeff));
end
end
##### 2 CommentsShow 1 older commentHide 1 older comment
lil brain on 15 Feb 2022
Speed is not too much of a issue here so no worries!