필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
lil brain
lil brain 2022년 2월 14일
댓글: lil brain 2022년 2월 15일
Dear community,
I have two cell array files. They are "distances_head_lefthand_windows" and "distances_head_righthand_windows".
They both look like this (in reality they are much larger with many more cells):
distances_head_lefthand_windows =
19×1 cell array
{512×54 double}
{512×50 double}
...
distances_head_righthand_windows =
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:
X1 = distances_head_lefthand_windows{1,1}(:,1);
Y1 = distances_head_righthand_windows{1,1}(:,1);
corr( X1 , Y1 )
X2 = distances_head_lefthand_windows{1,1}(:,2);
X2 = distances_head_righthand_windows{1,1}(:,2);
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)
Thanks for your help!
  댓글 수: 2
the cyclist
the cyclist 2022년 2월 15일
A few clarifying questions:
For the first cell in left & right, you have a 512x54 and a 512x53. Do you want to ignore the last column in the "left" matrix? Is the "left" matrix always the one with more columns, and is it always exactly one more?
How do you want the output stored? Keeping with the same example of the first cell, it seems that you will generate 53 correlation coefficients. Do you want correlations{1} to be a 1x53 vector of those coefficents, or something else?
lil brain
lil brain 2022년 2월 15일
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
the cyclist 2022년 2월 15일
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.
% Made up inputs. (Use your actual data instead, and rename everywhere)
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
lil brain
lil brain 2022년 2월 15일
Works like a charm! Thanks for the additional comments. Those make quite a difference.
lil brain
lil brain 2022년 2월 15일
Speed is not too much of a issue here so no worries!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by