%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Script 4 Working with cell arrays %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% rand('state',37); % set arbitrary seed for uniform draws randn('state',37); % set arbitrary seed for normal draws m1=reshape(repmat((1:1:8),4,1),32,1); %design first column n=length(m1); m2=2+1.4*randn(n,1); %design second column m3=randsample([0 1],n,true)'; %this samples randomly n elements from the % set [0 1] with replacement (drop "true" if you ever want to sample from a % set of mubers w/o replacement) M=[m1 m2 m3]; k=size(M,2); MC=mat2cell(M,4*ones(8,1),k); mc1=MC{1}; %inspect contents of first cell mc1c2=MC{1}(:,2); %get second column of first cell %Ex.1: MC1=MC; %to preserve the original MC for i=1:8 MC1{i}=sortrows(MC{i},3); %means sort the rows in MC{i} in order %of the elements in the third column end MC1=cell2mat(MC1); %Ex.2: Msum=zeros(8,1); %pre-allocate a zero vector to collect results for i=1:8 Msum(i)=sum(MC{i}(:,2)); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Working with unbalanced panels %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% load c:\mlab_tut\from_stata.raw; data = from_stata; clear from_stata; % describe contents of data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %1 runid running observation id %2 orig_solid original solicitor ID %3 amount contributions (2005 dollars) %4 gender solicitor Gender (1=male) %5 race solicitor Race (1=white) %6 height solicitor height (inches) %7 weight solicitor weight (lbs) %8 beauty beauty index %9 spunk sum of personality scores %10 gen_resp observed gender of respondent (1=male) %11 raceresp observed race of respondent(1=white) %12 avg_HHsize average household size (Census block) %13 inc000 Census tract median income (2005 dollars) %14 lottery fundraising package included lottery component data=sortrows(data,2); y = data(:,3); X = data(:,4:end); n=length(y); k=size(X,2); % get vector with panel sizes sol_id=data(:,2); sol_vec=[]; %assume we don't even know how many individuals there are, so % instead of pre-allocating a vector with a specific number of zeros we % simply pre-allocate an empty vector; j=0; for i=2:n if sol_id(i)~=sol_id(i-1)% check if subsequent elements in sol_id are % equal or not j=i-1-sum(sol_vec); %j will give the panel size sol_vec=[sol_vec;j]; end end sol_vec=[sol_vec;n-sum(sol_vec)]; % tag on size of last panel % make sure this worked: test=mat2cell(sol_id,sol_vec,1); test{1}; test{32}; test{end}; % now partition X into cells XC=mat2cell(X,sol_vec,k); %%%%%%%%%%%%% % Practice %%%%%%%%%%%%%%% %Ex 1: a=[0 3 5 0 3 5 0 3 5 0 3 5]'; a=sort(a); A=mat2cell(a,4*ones(3,1),1) %Ex2: for i=1:3 A{i}=A{i}'; end AA=cell2mat(A); %equivalently: AA=(reshape(a,4,3))'; %Ex3: rand('state',37); % set arbitrary seed for uniform draws randn('state',37); % set arbitrary seed for normal draws g1=randsample([1:1:5],30,true)'; g2=ones(30,1); g3=8*rand(30,1); g4=randn(30,1); G=[g1 g2 g3 g4]; n=length(G); k=size(G,2); G=sortrows(G,1); id=G(:,1); s=[]; j=0; for i=2:n if id(i)~=id(i-1)% check if subsequent elements in sol_id are % equal or not j=i-1-sum(s); %j will give the panel size s=[s;j]; end end s=[s;n-sum(s)]; % tag on size of last panel GC=mat2cell(G,s,k); sl=length(s); for i=1:sl GC{i}=sortrows(GC{i},4); end GC=cell2mat(GC);