@_ Many thanks for your help. Happy to accept if you post this as an answer.

I have also asked a simialr question that builds upon this where I have a rmaining part of the code and not sure how to apply cellfun there:

1 view (last 30 days)

Show older comments

Hi all,

I have two 21 x1 cells containg 100x18 tables (attached).

My aim is to calculate mean correlation coeffiient for each variable (18 variables) across all tables (21 tables) between to data sets. The below is my attempt to do is using an example of just one variable. Can this be written in a easier way?

load ('Data1'); %Load data

load('Data2');

n_tables_1 = numel(data1); % Data 1 - put all tables' data into a 3D array: 100x18x21

all_data_1 = zeros([size(data1{1}),n_tables_1]);

for ii = 1:n_tables_1

all_data_1(:,:,ii) = table2array(data1{ii});

end

n_tables_2 = numel(data2); % Data 2 - put all tables' data into a 3D array: 100x18x21

all_data_2 = zeros([size(data2{1}),n_tables_2]);

for ii = 1:n_tables_2

all_data_2(:,:,ii) = table2array(data2{ii});

end

%% Calculate r for variable 1

data1_var1 = all_data_1(:,1,:); % Data 1 - access variable 1

data2_var1 = all_data_2(:,1,:); % Data 2 - access variable 1

data1_var1 = reshape(data1_var1,[100,21]); % reshape to two-dimenasional matrix

data2_var1 = reshape(data2_var1,[100,21]);

r1=corrcoef(data1_var1(:,1),data2_var1(:,1)); % get r for each column until 21

r2=corrcoef(data1_var1(:,2),data2_var1(:,2));

r3=corrcoef(data1_var1(:,3),data2_var1(:,3));

r4=corrcoef(data1_var1(:,4),data2_var1(:,4));

r5=corrcoef(data1_var1(:,5),data2_var1(:,5));

etc.....

r21=corrcoef(data1_var1(:,21),data2_var1(:,21));

r_mean = (r1+r2+r3+....+r21)/21 %get mean r

%% Repeat the above for variables 2 to 21.

Voss
on 22 Mar 2022

See my answer below, which was for your similar previous question, which has since been deleted.

I realize this new question is not exactly the same, but this approach (using cellfun) may be of use:

load('Data1'); % t_sq_mtw

load('Data2'); % t_sq_dot_1

whos

% do polyfit() of P_acc_z_meancycle for each pair of tables

% (each pair is a table from t_sq_mtw and the corresponding table from t_sq_dot_1):

p = cellfun(@(x,y)polyfit(x.P_acc_z_meancycle,y.P_acc_z_meancycle,1), ...

t_sq_mtw,t_sq_dot_1,'UniformOutput',false);

% p is a 21-by-2 matrix of coefficients from polyfit(), p = [a1 a0]

p = cell2mat(p)

% do corrcoef() of P_acc_z_meancycle for each pair of tables:

% (each pair is a table from t_sq_mtw and the corresponding table from t_sq_dot_1):

r = cellfun(@(x,y)corrcoef(x.P_acc_z_meancycle,y.P_acc_z_meancycle), ...

t_sq_mtw,t_sq_dot_1,'UniformOutput',false);

% rSq is a 21-by-1 column vector of r-squared values from corrcoef():

rSq = cellfun(@(x)x(1,2)^2,r)

% calculate the mean and std of [p rSq] (= [a1 a0 rSq]) over all tables:

mean([p rSq],1) % mean of a1, a0, rSq, over all tables

std([p rSq],0,1) % standard deviation of a1, a0, rSq, over all tables

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

Start Hunting!