Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
There is a list of numbers, combine (paste) to single string with some connector, trim each element to eliminate spaces.
rng(123,'philox')
ar_rand = rand([5,1]);
st_fl_rand = string(num2str(ar_rand));
st_untrimmmed = strjoin(st_fl_rand, "#");
cl_st_trimmmed = cellfun(@(x) strtrim(x), cellstr(st_fl_rand), 'UniformOutput', false);
st_trimmmed = strjoin(string(cl_st_trimmmed), "#");
disp(['st_untrimmmed:' st_untrimmmed]);
"st_untrimmmed:" " 0.53162# 0.60704# 0.31843#0.0016474# 0.65784"
disp(['st_trimmmed:' st_trimmmed]);
"st_trimmmed:" "0.53162#0.60704#0.31843#0.0016474#0.65784"
the function below returns the position of cl_st_param_keys in ls_st_param_key should only include in cl_st_param_keys strings that also exist in ls_st_param_key.
ls_st_param_key = {'fl_crra', 'fl_beta', ...
'fl_w', 'fl_r_save', ...
'fl_a_max', 'it_z_n', 'it_a_n'};
cl_st_param_keys = {'fl_w', 'fl_beta', 'it_z_n'};
cell2mat(cellfun(@(m) find(strcmp(ls_st_param_key, m)), ...
cl_st_param_keys, 'UniformOutput', false))
ans = 1x3
3 2 6
cl_st_param_keys = {'fl_crra', 'fl_beta'};
param_tstar_map = containers.Map('KeyType','char', 'ValueType','any');
it_simu_vec_len = 5;
param_tstar_map('fl_crra') = linspace(1, 2, 5);
param_tstar_map('fl_beta') = linspace(0.94, 0.98, 10);
param_tstar_map('w') = linspace(1.1, 1.4, it_simu_vec_len);
param_tstar_map('r') = linspace(0.01, 0.04, it_simu_vec_len);
ar_it_array_len = cell2mat(cellfun(@(m) length(param_tstar_map(m)), ...
cl_st_param_keys, 'UniformOutput', false));
it_total_length = sum(ar_it_array_len);
disp(['ar_it_array_len: ' num2str(ar_it_array_len)])
ar_it_array_len: 5 10
disp(['it_total_length: ' num2str(it_total_length)])
it_total_length: 15
cl_st_param_keys = {'fl_crra', 'fl_beta'};
param_tstar_map = containers.Map('KeyType','char', 'ValueType','any');
it_simu_vec_len = 5;
param_tstar_map('fl_crra') = linspace(1, 2, 5);
param_tstar_map('fl_beta') = linspace(0.94, 0.98, 10);
param_tstar_map('w') = linspace(1.1, 1.4, it_simu_vec_len);
param_tstar_map('r') = linspace(0.01, 0.04, it_simu_vec_len);
rng(123);
it_simu_length = 20;
mt_param_rand = cell2mat(cellfun(@(m) ...
rand([it_simu_length,1]).*(max(param_tstar_map(m)) - min(param_tstar_map(m))) ...
+ min(param_tstar_map(m)), ...
cl_st_param_keys, 'UniformOutput', false));
tb_rand_draws = array2table(mt_param_rand, 'VariableNames', cl_st_param_keys);
disp(tb_rand_draws);
fl_crra fl_beta
_______ _______
1.5316 0.97337
1.607 0.97305
1.3184 0.94644
1.0016 0.97349
1.6578 0.94983
1.7505 0.97152
1.7407 0.94277
1.7108 0.94781
1.3542 0.97625
1.1479 0.95709
1.8834 0.94962
1.1274 0.96042
1.2132 0.96637
1.0676 0.94936
1.4318 0.96911
1.3791 0.97365
1.9399 0.94242
1.6369 0.96946
1.9791 0.95752
1.5709 0.96145