How to use cellfun?
    13 次查看(过去 30 天)
  
       显示 更早的评论
    
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()
0 个评论
采纳的回答
  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]);
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
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

