-
Notifications
You must be signed in to change notification settings - Fork 10
/
fs_cellfuns.m
68 lines (53 loc) · 2.89 KB
/
fs_cellfuns.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%% Matlab List Comprehension with Cells
% *back to* <https://fanwangecon.github.io *Fan*>*'s* <https://fanwangecon.github.io/Math4Econ/
% *Intro Math for Econ*>*,* <https://fanwangecon.github.io/M4Econ/ *Matlab Examples*>*,
% or* <https://fanwangecon.github.io/MEconTools/ *MEconTools*> *Repositories*
%% Concatenate Numeric Values as String with Trimming
% 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]);
disp(['st_trimmmed:' st_trimmmed]);
%% Find Index of Elements of String Cells in a larger String Cells
% 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))
%% Given Container of Arrays, Find Total Length of All Arrays for Selected Keys
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)])
disp(['it_total_length: ' num2str(it_total_length)])
%% Given Container of Arrays, Find Min and Max of Each and Draw Random N sets
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);