Convert the following matlab code to R : %Cohorts should be 1D array of structs with these fields:
    %Carbon pools
    %litterC  				%Carbon substrate. Relative amounts of C species are important for resp rate
    %litterN  				%Nitrogen substrate
    %protectedC 				%Aggregate and mineral complex C
    %protectedN 				%Aggregate and mineral complex C
    %livingMicrobeC
    %livingMicrobeN             					 %Current carbon mass of live microbes
    %CO2                       					 %Cumulative CO2 generated by decomposition
    %Rtot                        				 %Cumulative decomposition (includes double counting through microbial products)
    %Original C, for conservation checks
    %originalLitterC
    %originalLitterN         					  !NAME_1 track for carbon balance check



function [cohorts,outputs] = update_cohorts(cohorts,T,theta,mineralN,dt,params)

% if length(cohorts) ~= length(mineralN)
%     error('Length of cohorts must equal length of mineralN')
% end

ncohorts=45;



mineralN=mineralN(:);

if T<0
    error('T<0')
end
if theta<0 || theta>1
    error('Theta must be between 0 and 1')
end
if any(mineralN<0)
    error('mineralN<0')
end

% global params;
totalResp=zeros(ncohorts,3);
totalN_decomposed=zeros(ncohorts,3);


et=params.et;
eup=repmat(params.eup,[ncohorts,1]);

cohorts_array=cohorts;
check_cohorts(cohorts_array);

totalN_start=sum(mineralN)+sum(sum(cohorts_array.litterN))+sum(sum(cohorts_array.protectedN))+sum(cohorts_array.livingMicrobeN);
totalC_start=sum(sum(cohorts_array.litterC))+sum(sum(cohorts_array.protectedC))+sum(cohorts_array.livingMicrobeC)+sum(cohorts_array.CO2);

%Calculate maximum potential C decomposition rate
potential_tempResp=Resp(cohorts_array.litterC,cohorts_array.livingMicrobeC,T,theta);

%Carbon loss cannot exceed size of pool
potential_tempResp(dt.*potential_tempResp > cohorts_array.litterC) = cohorts_array.litterC(dt.*potential_tempResp > cohorts_array.litterC)/dt;

%N associated with the C decomposition rate
pot_tempN_decomposed = potential_tempResp.*cohorts_array.litterN./cohorts_array.litterC; %kgN/m2/yr
pot_tempN_decomposed(cohorts_array.litterC<=0.0)=0.0;


%Maximum N immobilization rate
IMM_N_max=mineralN*params.max_immobilization_rate*365;   %kg/m2/yr
xx=IMM_N_max>mineralN/dt;
IMM_N_max(xx)=mineralN(xx)/dt;

microbeTurnover=max(0.0,(cohorts_array.livingMicrobeC-params.minMicrobeC*sum(cohorts_array.litterC,2))/params.Tmic);   %kg/m2/yr


% Carbon and nitrogen available for microbial growth
carbon_supply=sum(potential_tempResp.*repmat(params.eup,[ncohorts,1]),2);
nit Limit your response to 150 words.
user