# Can this code run faster?

5 views (last 30 days)
Max Ellens on 9 Jun 2020
Answered: Nam Vu on 9 Jun 2020
I currently have a code, that I have to run a 100+ times, with 100+ different seeds. But only one round already takes 72 seconds, and my fellow students who run the code in R say their code runs a lot faster. particularly the fitlm part of part 5 of the code takes up some time. Does anyone know how to make the following code faster or more effective?
tic
seed = 2;
1 %%
rng('default');
rng(seed);
x = zeros(4000,1);
sz = size(x);
Income = lognrnd(3.60,0.5, sz);
v = normrnd(0,1,sz);
Beta = zeros(4000,1);
B0 = -2;
B1 = 1;
for i = 1:4000
Beta(i) = B0 + B1*(Income(i)/100000) + v(i);
end
2 %%
rng(seed);
HouseholdSize = zeros(4000,1);
sz = size(HouseholdSize);
for i = 1:800
x = randi([1 2]);
HouseholdSize(i) = x;
end
for i = 801:1600
x = randi([1 3]);
HouseholdSize(i) = x;
end
for i = 1601:2400
x = randi([1 4]);
HouseholdSize(i) = x;
end
for i = 2401:3200
x = randi([1 5]);
HouseholdSize(i) = x;
end
for i = 3201:4000
x = randi([1 6]);
HouseholdSize(i) = x;
end
HouseholdSize = HouseholdSize(randperm(length(HouseholdSize)));
u = normrnd(0,0.1,sz);
Rho = zeros(4000,1);
P0 = 0.7;
P1 = 0.015;
for i = 1:4000
Rho(i) = P0 + P1*HouseholdSize(i) + u(i);
end
3 %%
rng(seed)
X = 0.5 + (1).*rand(4000,24);
q = zeros(4000,1);
sizeq = size(q);
Alpha = normrnd(10,0.1,sizeq);
y = zeros(4000,24);
y(:,1) = 10;
sizey = size(y);
epsilon = normrnd(0,1,sizey);
for hh = 1:4000
for time = 2:24
y(hh,time) = Alpha(hh) + Beta(hh)*X(hh,time) + Rho(hh)*y(hh,time-1) + epsilon(hh,time);
end
end
4 %%
SamenTabel = zeros(92000,5);
for i = 1:4000
for j = 1:23
SamenTabel((i-1)*23 + j,1) = j + 1;
SamenTabel((i-1)*23 + j,2) = i;
SamenTabel((i-1)*23 + j,3) = y(i, j + 1);
SamenTabel((i-1)*23 + j,4) = X(i, j);
SamenTabel((i-1)*23 + j,5) = y(i, j);
end
end
SamenTabel = array2table(SamenTabel);
5 %%
tic
Amount1 = unique(SamenTabel(:,2));
Amount = height(Amount1);
Dataset1 = table2array(SamenTabel);
Dataset = cell(Amount,3);
SimuAlpha = zeros(4000,2);
SimuBeta = zeros(4000,2);
SimuRho = zeros(4000,2);
for i = 1:1:Amount
[Rows, ~] = find(Dataset1(:,2) == i);
DatasetUSE = SamenTabel(Rows,:);
Regression = fitlm(DatasetUSE,...
Dataset(i,1) = {Regression};
T1 = table2array(Dataset{i, 1}.Coefficients(1,1));
T11 = table2array(Dataset{i, 1}.Coefficients(1,2));
T2 = table2array(Dataset{i, 1}.Coefficients(2,1));
T22 = table2array(Dataset{i, 1}.Coefficients(2,2));
T3 = table2array(Dataset{i, 1}.Coefficients(3,1));
T33 = table2array(Dataset{i, 1}.Coefficients(3,2));
SimuAlpha(i,1) = T1;
SimuAlpha(i,2) = T11;
SimuBeta(i,1) = T2;
SimuBeta(i,2) = T22;
SimuRho(i,1) = T3;
SimuRho(i,2) = T33;
end
toc
6 %%
Laag2 = zeros(4000,2);
Laag3 = zeros(4000,2);
for i = 1:4000
Laag2(i,1) = SimuBeta(i,1);
Laag2(i,2) = Income(i,1);
Laag3(i,1) = SimuRho(i,1);
Laag3(i,2) = HouseholdSize(i,1);
end
Laag2 = array2table(Laag2);
Laag3 = array2table(Laag3);
Laag2.Properties.VariableNames = {'Beta' 'Income'};
Laag3.Properties.VariableNames = {'Rho' 'Size'};
7 %%
BetaEstimate = fitlm(Laag2,...
'Beta ~ 1 + Income');
RhoEstimate = fitlm(Laag3,...
'Rho ~ 1 + Size');
%%
toc

Michael W on 9 Jun 2020
Use the MATLAB profiler to find out where your code is slow.

Nam Vu on 9 Jun 2020
I've run #2, I think you could review line by line to do the code-refactoring. For example:
for i = 1:800
x = randi([1 2]);
HouseholdSize(i) = x;
end
could be :
for i = 1:800
HouseholdSize(i) = randi([1 2]);
end
less 1 assignment command => more faster.
For thousands of runs, I think we have another way to create the HouseholdSize:
HouseholdSize = [randi([1 2], [800,1]) randi([1 3], [800,1]) randi([1 4], [800,1]) randi([1 5], [800,1]) randi([1 6], [800,1])];
HouseholdSize = HouseholdSize(randperm(4000));
You could use the vector-operator to do the calculate Rho:
u = normrnd(0,0.1,[1,4000]);
P0 = 0.7;
P1 = 0.015;
Rho = P0 + P1*HouseholdSize +u
Regards,

R2019a

### Community Treasure Hunt

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

Start Hunting!