Can this be written in a easier way?

1 visualización (últimos 30 días)
Tomaszzz
Tomaszzz el 21 de Mzo. de 2022
Comentada: Voss el 22 de Mzo. de 2022
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.
  2 comentarios
Tomaszzz
Tomaszzz el 22 de Mzo. de 2022
Editada: Tomaszzz el 22 de Mzo. de 2022
@_ 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:
Voss
Voss el 22 de Mzo. de 2022
@Tomaszzz I have moved the comment to an answer. Thanks! I will look at the other question too.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 22 de Mzo. de 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
Name Size Bytes Class Attributes ans 1x34 68 char t_sq_dot_1 21x1 411789 cell t_sq_mtw 21x1 411789 cell
% 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)
p = 21×2
0.9867 0.1336 1.0177 0.1576 0.9638 0.1300 -0.4362 0.0891 0.7685 0.1178 0.5379 0.1677 1.0999 0.1258 0.9652 0.1466 0.9787 0.2405 1.2832 0.4761
% 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)
rSq = 21×1
0.9849 0.9866 0.8919 0.3034 0.8005 0.2836 0.9708 0.8687 0.9272 0.6527
% 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
ans = 1×3
0.8564 0.1619 0.7536
std([p rSq],0,1) % standard deviation of a1, a0, rSq, over all tables
ans = 1×3
0.4049 0.0889 0.2948

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by