How to use cellfun?

21 ビュー (過去 30 日間)
Tomaszzz
Tomaszzz 2022 年 3 月 22 日
コメント済み: Voss 2022 年 3 月 22 日
Hi all,
I have two 21 x1 cells (t_sq_mtw / t_sq_dot_1) containg 100x18 tables.
The below code calculates three parameters (a0_longhand/ a1_longhand/rSq_longahnd) for one variable (P_acc_z_meancycle) from correspoding first pair of tables.
I was wondering how could I apply cellfun to apply the code to all tables (not just one as in the code below)? Can you help please?
Pref = (t_sq_mtw{1, 1}.P_acc_z_meancycle); %variable one from data set 1
Pa = (t_sq_dot_1{1, 1}.P_acc_z_meancycle); %%variable one from data set 2
for i=1:length(Pref)
temp_toprow(i)=(Pref(i,1)-mean(Pref))*(Pa(i,1)-mean(Pa));
temp_bottomrow(i)=(Pref(i,1)-mean(Pref))^2;
end
toprow=sum(temp_toprow);
bottomrow=sum(temp_bottomrow);
a1_longhand=toprow/bottomrow;
a0_longhand=mean(Pa)-(a1_longhand*mean(Pref));
n=length(Pa);
x=Pref;
y=Pa;
toprow=(n*(sum(y.*x)))-(sum(x)*sum(y));
bottomrow=sqrt(((n*sum(x.^2))-(sum(x))^2)*((n*sum(y.^2))-(sum(y))^2));
r=toprow/bottomrow;
rSq_longhand=r^2;
if a1~=a1_longhand
warning(1)=1;
else
warning(1)=0;
end
if a0~=a0_longhand
warning(2)=1;
else
warning(2)=0;
end
if rSq~=rSq_longhand
warning(3)=1;
else
warning(3)=0;
end
This is the first part of the code were cellfun is sucessfully used.
% do polyfit() of P_acc_z_meancycle for each pair of tables
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 = cell2mat(p); % p is a 21-by-2 matrix of coefficients from polyfit()
a1 = p(:,1);
a0 = p(:,2);
% do corrcoef() of P_acc_z_meancycle for each pair of tables:
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 = cellfun(@(x)x(1,2)^2,r); % rSq is a 21-by-1 column vector of r-squared values from corrcoef()

採用された回答

Voss
Voss 2022 年 3 月 22 日
You can make that code into its own function and then use cellfun() to call that function on all pairs of tables:
load('Data1.mat'); % t_sq_mtw
load('Data2.mat'); % t_sq_dot_1
% call get_longhand() on each pair of tables' P_acc_z_meancycle
[a1_longhand,a0_longhand,rSq_longhand] = cellfun( ...
@(x,y)get_longhand(x.P_acc_z_meancycle,y.P_acc_z_meancycle), ...
t_sq_mtw,t_sq_dot_1);
disp([a1_longhand a0_longhand rSq_longhand]);
0.9867 0.1336 0.9849 1.0177 0.1576 0.9866 0.9638 0.1300 0.8919 -0.4362 0.0891 0.3034 0.7685 0.1178 0.8005 0.5379 0.1677 0.2836 1.0999 0.1258 0.9708 0.9652 0.1466 0.8687 0.9787 0.2405 0.9272 1.2832 0.4761 0.6527 0.9320 0.1208 0.9839 0.9377 0.0799 0.8746 0.9965 0.1056 0.9026 0.0039 0.1945 0.0000 1.0158 0.1026 0.9746 1.0483 0.1801 0.9568 1.2828 0.2525 0.4985 0.9924 0.1191 0.9674 0.9760 0.1164 0.9577 0.5547 0.0885 0.3260 1.0794 0.2551 0.7127
function [a1_longhand,a0_longhand,rSq_longhand] = get_longhand(Pref,Pa)
for i=1:length(Pref)
temp_toprow(i)=(Pref(i,1)-mean(Pref))*(Pa(i,1)-mean(Pa));
temp_bottomrow(i)=(Pref(i,1)-mean(Pref))^2;
end
toprow=sum(temp_toprow);
bottomrow=sum(temp_bottomrow);
a1_longhand=toprow/bottomrow;
a0_longhand=mean(Pa)-(a1_longhand*mean(Pref));
n=length(Pa);
x=Pref;
y=Pa;
toprow=(n*(sum(y.*x)))-(sum(x)*sum(y));
bottomrow=sqrt(((n*sum(x.^2))-(sum(x))^2)*((n*sum(y.^2))-(sum(y))^2));
r=toprow/bottomrow;
rSq_longhand=r^2;
end
  2 件のコメント
Tomaszzz
Tomaszzz 2022 年 3 月 22 日
Many thanks!
Voss
Voss 2022 年 3 月 22 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by