• Data Structures and Dynamic Optimization with Matlab
  • Preface
  • 1 Data Structures
    • 1.1 Matrices and Arrays
      • 1.1.1 Array Reshape, Repeat and Expand Examples
      • 1.1.2 Array Index Slicing and Subsetting to Replace and Expand
      • 1.1.3 Maximum of Matrix Columns, Sort Matrix Columns
      • 1.1.4 Array Broadcast and Expansion Examples
      • 1.1.5 Grid States, Choices and Optimal Choices Example
      • 1.1.6 Accumarray Examples
      • 1.1.7 Matlab Miscellaneous and Basic Numeric and Array Operations
    • 1.2 ND Dimensional Arrays
      • 1.2.1 Joint Arrays All Combinations and by Random Subset
      • 1.2.2 3D, 4D, ND Arrays Reshape and Rearrange Dimensions
      • 1.2.3 Multidimensional ND Array to 2D Matrix with Wide to Long
    • 1.3 Cells
      • 1.3.1 Matlab Cell Array Basic Operations
      • 1.3.2 Matlab List Comprehension with Cells
      • 1.3.3 All Possible Combinations of Multiple Arrays
      • 1.3.4 Nested Cells
    • 1.4 Characters and Strings
      • 1.4.1 Basic String Operations, Display, Search, Join and Split
      • 1.4.2 String Array Manipulations, Join, Find, Replace and the Alphabet
      • 1.4.3 Convert and Concatenate Strings Arrays with Numbers and Number Arrays with Strings
    • 1.5 Map Containers
      • 1.5.1 Container Map Basics
      • 1.5.2 Container Map Display Swtich Key and Values and Subseting
      • 1.5.3 Container Map Example Overriding
    • 1.6 Map Structure Array
      • 1.6.1 Maplab Nested Container Map with Struct
  • 2 Functions and Programming
    • 2.1 Development and Debugging
      • 2.1.1 Matlab Error and Warning Handling
      • 2.1.2 Matlab Profiler Save to HTML
    • 2.2 varargin Default Parameters
      • 2.2.1 varargin as a Function Parameter
      • 2.2.2 Container Default Parameter with varargin
    • 2.3 Dynamic Functions
      • 2.3.1 Anonymous Function in Matlab
      • 2.3.2 Dynamically Generate M Files
  • 3 Distributional Processes
    • 3.1 Time Series
      • 3.1.1 Simulate AR(1) Autoregressive Processes
      • 3.1.2 Matlab Moving Average of Time-series
    • 3.2 Cross-sectional Data
      • 3.2.1 Matlab Mincer Wage Earnings Equation with Experience, Education and Gamma Shocks
  • 4 Simulation
    • 4.1 Normal Distribution
      • 4.1.1 Compute CDF for Normal and Bivariate Normal Distributions
      • 4.1.2 Cholesky Decomposition Correlated Bivariate Normal from IID Random Draws
      • 4.1.3 Cholesky Decomposition Correlated Five Dimensional Multivariate Normal Shock
  • 5 Estimation
    • 5.1 Linear Estimation
      • 5.1.1 Linear OLS Regression
    • 5.2 Nonlinear Estimation
      • 5.2.1 Nonlinear Estimation with Fminunc
  • 6 Graphs
    • 6.1 Figure Components
      • 6.1.1 Matlab Graph Safe Colors for Web, Presentation and Publications Examples
      • 6.1.2 Matlab Graph Titling, Labels and Legends Examples
      • 6.1.3 Matlab Graph Matrix with Jet Spectrum Color, Label a Subset Examples
    • 6.2 Basic Figure Types
      • 6.2.1 Matlab Graph Scatter Plot Examples
      • 6.2.2 Matlab Line and Scatter Plot with Multiple Lines and Axis Lines
      • 6.2.3 Matlab Graph Scatter and Line Spectrum with Three Variables
    • 6.3 Graph Functions
      • 6.3.1 Matlab Graph One Variable Function
    • 6.4 Write and Read Plots
      • 6.4.1 Matlab Graph Generate EPS postscript figures in matlab
  • 7 Tables
    • 7.1 Basic Table Generation
      • 7.1.1 Generate a Table and Fill with Data Row by Row or Random Data
      • 7.1.2 Tables Order, Sort, Add, Rename and Drop Columns
      • 7.1.3 Row and Column Names for Table based on Arrays
      • 7.1.4 Select Subset of Rows and Columns
    • 7.2 Table Joining
      • 7.2.1 Matlab Join Tables by Keys
      • 7.2.2 Row and Column Combine Stack Tables and Matrices
      • 7.2.3 Matlab Table Stack and Join Estimation and Simulation Results
    • 7.3 Summarize
      • 7.3.1 Matlab Table Summarize and Aggregate by Groups
  • Appendix
  • A Index and Code Links
    • A.1 Data Structures links
      • A.1.1 Section 1.1 Matrices and Arrays links
      • A.1.2 Section 1.2 ND Dimensional Arrays links
      • A.1.3 Section 1.3 Cells links
      • A.1.4 Section 1.4 Characters and Strings links
      • A.1.5 Section 1.5 Map Containers links
      • A.1.6 Section 1.6 Map Structure Array links
    • A.2 Functions and Programming links
      • A.2.1 Section 2.1 Development and Debugging links
      • A.2.2 Section 2.2 varargin Default Parameters links
      • A.2.3 Section 2.3 Dynamic Functions links
    • A.3 Distributional Processes links
      • A.3.1 Section 3.1 Time Series links
      • A.3.2 Section 3.2 Cross-sectional Data links
    • A.4 Simulation links
      • A.4.1 Section 4.1 Normal Distribution links
    • A.5 Estimation links
      • A.5.1 Section 5.1 Linear Estimation links
      • A.5.2 Section 5.2 Nonlinear Estimation links
    • A.6 Graphs links
      • A.6.1 Section 6.1 Figure Components links
      • A.6.2 Section 6.2 Basic Figure Types links
      • A.6.3 Section 6.3 Graph Functions links
      • A.6.4 Section 6.4 Write and Read Plots links
    • A.7 Tables links
      • A.7.1 Section 7.1 Basic Table Generation links
      • A.7.2 Section 7.2 Table Joining links
      • A.7.3 Section 7.3 Summarize links
  • M4Econ Bookdown

Data Structures and Dynamic Optimization with Matlab

A Index and Code Links

A.1 Data Structures links

A.1.1 Section 1.1 Matrices and Arrays links

  1. Array Reshape, Repeat and Expand: mlx | m | pdf | html
    • Reshape and flatten arrays.
    • m: reshape()
  2. Array Index Slicing and Subsetting to Replace and Expand: mlx | m | pdf | html
    • Index based column and row expansions.
    • Anonymous function to slice array subsets.
    • m: sub2ind() + @(it_subset_n, it_ar_n) unique(round(((0:1:(it_subset_n-1))/(it_subset_n-1)) times (it_ar_n-1)+1))
  3. Find the Maximum Value and Index in Matrix Over Columns and Overall: mlx | m | pdf | html
    • Given 2D array, find the maximum value and index for each column.
    • Find the maximum value in a 2D array’s row and column indexes.
    • m: max() + ind2sub() + maxk()
  4. Array Broadcasting Examples: mlx | m | pdf | html
    • broadcast means: array + array’ + matrix = matrix.
  5. Grid States, Choices and Optimal Choices Example: mlx | m | pdf | html
    • States, choices, and find max.
  6. Accumarray Examples: mlx | m | pdf | html
    • Accumarray to sum up probabilities/values for discrete elements of arrays.
    • m: unique() + reshape() + accumarray()
  7. Matlab Miscellaneous Array and Numeric Operations: mlx | m | pdf | html
    • Loop over numbers, find modulus (remainder) and quotient given divisor.
    • Check data and parameter types.
    • Compare approximately similar values.
    • Find imaginary elements of array.
    • m: imag() + isfloat() + iscell() + isnan() + isnumeric()

A.1.2 Section 1.2 ND Dimensional Arrays links

  1. All Possible Combinations of Arrays as Table or Random Subset Mesh: mlx | m | pdf | html
    • Generate a Table based on all possible combinations of several arrays.
    • Draw randomly from array, permutate arrays.
    • m: ndgrid() + cell2mat(cellfun(@(m) m(:), cl_mt_all, ‘uni,’ 0))
  2. 3D, 4D, ND Arrays Reshape and Summarize: mlx | m | pdf | html
    • Slice 2D matrixes out of ND matrixes. The 2D matrix is contiguous, but can be intermediate dimensions.
    • Summarize a nd dimensional matrix along one or two dimensions group by various other dimensions.
    • m: permute(mn, [3,1,2,4]) + squeeze(num2cell(mn, [1,2])) + celldisp() + ndgrid()
  3. ND Array Wide to Long Reshape to Table Dataframe with Variable Values for Each Dimension: mlx | m | pdf | html
    • Given 2D policy function f(a,z), generate table/dataframe with a, z, and f(a,z) columns.
    • There is a ND Array where each dimension is a different attribute, generate 2D dataframe with columns for attribute values and ND Array values stored as a single column.
    • There might be many NaN values in the ND array, drop NaN values in the ND array for 2D dataframe. Find the non-NaN values along each index dimension.
    • m: cell() + NaN() + isnan() + ind2sub() + find()

A.1.3 Section 1.3 Cells links

  1. Combine Cells: mlx | m | pdf | html
    • Combine string cell arrays and string.
    • m: [{st_param}, ls_st_param_key, cl_st_param_keys]
  2. List Comprehension with Cells: mlx | m | pdf | html
    • Cell2mat, cellfun, anonymous function list comprehension over cells.
    • Find min and max of all arrays in cells.
    • Find length of all arrays in cells; find index of elements of one array in another cell array.
    • Trim and concatenate floats to single string.
    • m: cell2mat() + cellfun() + strcmp() + strtrim() + find() + cell2mat(cellfun(@(m) find(strcmp(ls_st_param_key, m)), cl_st_param_keys, ‘UniformOutput,’ false)) + cellfun(@(x) strtrim(x), cellstr(st_fl_rand), ‘UniformOutput,’ false)
  3. Permutate Cells: mlx | m | pdf | html
    • Generate all possible combinations of various arrays contained in cell array.
    • m: ndgrid() + cell2mat() + array2table() + cell2mat(cellfun(@(m) m(:), cl_mt_all, ‘uni,’ 0))
  4. Nested Cells: mlx | m | pdf | html
    • Cell of cells with inner cell having multiple types.
    • m: linspace() + cell([4,1]) + clns_parm_tstar{1} = {‘fl_crra,’ ‘CRRA,’ linspace(1, 2, it_simu_vec_len)} + disp(clns_parm_tstar(1)) + disp(clns_parm_tstar{1}{1})

A.1.4 Section 1.4 Characters and Strings links

  1. Basic String Operations, DisplayDisplay, Search, Join and Split: mlx | m | pdf | html
    • Generating a string for file suffix based on date and time.
    • Print info segment of multiple numeric and string parameters, scalar and array.
    • Compose string and rounded numeric array.
    • Cut string suffix and append new suffix.
    • m: *datestr(now, ‘mm-dd-yyyy-HH-MM’) + char() + compose() + strjoin() + contains() + matches() + str_sub = split(string, “.”) + strcat(str_sub{1}, ’_m.m’)*
  2. String Arrays Operations, Join, Find, Replace and the Alphabet: mlx | m | pdf | html
    • Generate string arrays and cell strings.
    • String array from single and double quoted strings.
    • Duplicate strings, concatenate string, and paste strings jointly with separator.
    • Find string element positions, replace substrings.
    • m: repmat() + num2str() + string() + strcat() + strjoin() + fprintf() + strcmp() + strrep() + cel2mat(cellfun(@(m) find(strcmp()))) + cellstr() + ‘a’:‘z’
  3. Convert and Cancatenate String and Numeric Array Concatenations: mlx | m | pdf | html
    • Generate rounded string array matrix with leading zero, leading space, decimal round from numeric matrix.
    • Generate a string from a numeric array and join with positional counter.
    • Create a title string by joining rounded parameter and parameter names, with decimal formatting.
    • Concatenate multiple numeric arrays together with strings and format.
    • m: num2str() + compose() + cellstr() + strcat() + strjoin() + %.2f

A.1.5 Section 1.5 Map Containers links

  1. Container Map Basics: mlx | m | pdf | html
    • Numeric container map, dynamically filled container map.
    • Numeric scalar, string, matrix as values for map container.
    • Get values for multiple keys in map.
    • m: isKey() + strjoin() + containers.Map(‘KeyType,’ ‘char,’ ‘ValueType,’ ‘any’) + map.keys() + map.values() + values(param_map, {‘share_unbanked_j,’ ‘equi_r_j’})
  2. Container Map Display Swtich Key and Values and Subseting: mlx | m | pdf | html
    • Loop over map, display keys and values.
    • Select Container map subset by keys.
    • Generate new container map by switching the values to keys and keys to values.
    • m: strjoin() + keys(map) + values(map) + containers.Map(keys, values) + cellfun(@(x) num2str(x(:)), num_cell, ‘uni,’ 0);
  3. Cell Override: mlx | m | pdf | html
    • Override default map with externally fed map, update existing and add new keys.
    • m: param_map_updated = [param_map_old; param_map_updates_new]

A.1.6 Section 1.6 Map Structure Array links

  1. Struct of Map Container for Nested Value Retrieval: mlx | m | pdf | html
    • There is a list of parameters, use several container maps to store information about parameters, and combine them in a struct.
    • Use struct to in effect make single-line nested container map calls.
    • m: struct

A.2 Functions and Programming links

A.2.1 Section 2.1 Development and Debugging links

  1. Matlab Errors and Warnings: mlx | m | pdf | html
    • Turn off warning messages.
    • m: lastwarn + warning(‘off,’ st_warn_id);
  2. Matlab Profiling and Testing to Improve Code Speed: mlx | m | pdf | html
    • Profile code segment and save profiling results to HTML folder in the current directory.
    • m: profile on + profile off + profsave(profile(‘info’), spn) + matlab.desktop.editor.getActiveFilename + fileparts() + fullfile()

A.2.2 Section 2.2 varargin Default Parameters links

  1. Use varargin as a Function Parameter: mlx | m | pdf | html
    • Default parameters allow for maintaining code testability.
    • Use varargin for functions with limited parameters.
    • m: varargin + cell2mat() + function [out_put] = func_name(varargin)
  2. Container Default Parameter with varargin and Input Type Check: mlx | m | pdf | html
    • The varargin structure could lead to excessive code lines. Container Map works well with large parameter structure.
    • Core model functions with potentially many parameters, possibly override default generation to save time.
    • m: varargin + function [out_put] = func_name(varargin) + cm_defaults = {cm_a, cm_b} + [cm_defaults{1:optional_params_len}] = varargin{:} + cm_c = [cm_a;cm_b]

A.2.3 Section 2.3 Dynamic Functions links

  1. Anonymous Function Examples: mlx | m | pdf | html
    • Define a wage equation where individuals working part time earn a fraction of the full time earnings.
    • m: gamrnd() + f_x = @(x) x
  2. Dynamically Generate M File: mlx | m | pdf | html
    • Get current working file file name and path.
    • Generate a m file from strings, add file to path, can call that m file.
    • m: matlab.desktop.editor.getActiveFilename + fileparts() + fullfile() + addpath() + fopen() + fprintf() + fclose()

A.3 Distributional Processes links

A.3.1 Section 3.1 Time Series links

  1. Autoregressive Process AR(1): mlx | m | pdf | html
    • The Mean and standard deviation of an AR(1) process.
    • Simulate and graph an AR(1) persistent process.
    • Simulate log income process with parameters estimated from Indian income data.
    • m: normrnd() + for it_t=1:1:length(ar_shk) + plot(ar_t, ar_y)
  2. Moving Average of Neighboring Values: mlx | m | pdf | html
    • Compute moving average of surrounding values with different windows.
    • Visualize moving averages compare actual to smoothed average to fully flat moving average.
    • m: movmean()
    • MEconTools: ff_graph_grid()

A.3.2 Section 3.2 Cross-sectional Data links

  1. Mincer Wage Earnings Equation with Experience, Education and Gamma Shocks: mlx | m | pdf | html
    • Define a wage equation where individuals working part time earn a fraction of the full time earnings.
    • Wage at different education and experience levels.
    • Simluate wage with an array of gamma distribution shocks.
    • m: gamrnd() + f_x = @(x) x + histogram()
    • MEconTools: ff_graph_grid + ff_simu_stats

A.4 Simulation links

A.4.1 Section 4.1 Normal Distribution links

  1. Compute CDF for Normal and Bivariate Normal Distributions: mlx | m | pdf | html
    • CDF for normal random variable through simulation and with NORMCDF function.
    • CDF for bivariate normal random variables through simulation and with NORMCDF function, using cholesky deomposition to model correlation from uniform random draws.
    • m: mvncdf + norminv
  2. Cholesky Decomposition Correlated Two Dimensional Normal Shock: mlx | m | pdf | html
    • Draw two correlated normal shocks using the MVNRND function.
    • Draw two correlated normal shocks from uniform random variables using Cholesky Decomposition.
    • m: mvnrnd + corrcoef + norminv
  3. Cholesky Decomposition Correlated Five Dimensional Normal Shock: mlx | m | pdf | html
    • Generate variance-covariance matrix from correlation and standard deviation.
    • Draw five correlated normal shocks using the MVNRND function.
    • Draw five correlated normal shocks from uniform random variables using Cholesky Decomposition.
    • m: mvnrnd + corrcoef + norminv + subplot

A.5 Estimation links

A.5.1 Section 5.1 Linear Estimation links

  1. Estimate and Solve for Parameters in Linear System of Equation and OLS Regression: mlx | m | pdf | html
    • Fit a line through the origin with two points of data.
    • Solve/estimate an exactly identified system of linear equations.
    • m: fitlm() + fc_ols_lin = @(y, x) (x’x)^(-1)(x’y);

A.5.2 Section 5.2 Nonlinear Estimation links

  1. Matlab Simple Nonlinear Estimation: mlx | m | pdf | html
    • Nonlinear estimation using fminunc.
    • m: optimset() + fminunc()

A.6 Graphs links

A.6.1 Section 6.1 Figure Components links

  1. Image Pick Safe Colors: mlx | m | pdf | html
    • Display safe colors.
    • m: blue = [57 106 177]./255 + fill(x, y, cl_colors{it_color})
  2. Figure Titling and Legend: mlx | m | pdf | html
    • Multi-line titles, add legend lines.
    • Add to legend, select legend to show.
    • m: title({‘Cash-on-Hand’ ‘\(\alpha + \beta = \zeta\)’},‘Interpreter,’‘latex’) + legend([g1, g2, g3], {‘near,’‘linear,’‘spline’}, ‘Location,’ ‘best,’ ‘NumColumns,’ 1, ‘FontSize,’ 12, ‘TextColor,’ ‘black’);
  3. Graph Many Lines Legend for Subset: mlx | m | pdf | html
    • State-space plots with color spectrum: can not show all states in legend, show subset, add additional line to plot and legend.
    • m: jet() + numel() + fliplr() + jet(numel(chart)), set(chart(m), ‘Color,’ clr(m,:))

A.6.2 Section 6.2 Basic Figure Types links

  1. Scatter Plot Examples: mlx | m | pdf | html
    • Scatter multiple lines different colors, shapes and sizes.
    • m: scatter(x, y, size) + Marker + MarkerEdgeColor + MarkerEdgeAlpha + MarkerFaceColor + MarkerFaceAlpha
  2. Scatter Plot Examples: mlx | m | pdf | html
    • Scatter and lines multiple lines different colors, shapes and sizes.
    • X axis, Y axis, and 45 degree line.
    • m: xline(0) + yline(0) + refline([1 0]) + plot(x,y) + HandleVisibility + Color + LineStyle + LineWidth
  3. Three variables Scatter and Lines with Color Spectrum: mlx | m | pdf | html
    • Two dimensional matrix for x and y, a third variable with color spectrum set via loop.
    • m: plot(2d, 2d) + jet + set(chart(m), ‘Color,’ clr)

A.6.3 Section 6.3 Graph Functions links

  1. Matlab Plot Polynomials, Budget and Indifference Functions: mlx | m | pdf | html
    • Use fplot to plot a one variable function.
    • Plot budget constraint and indifference curve.
    • m: fplot() + xline() + yline() + title([char(f_x)],‘Interpreter,’“none”);

A.6.4 Section 6.4 Write and Read Plots links

  1. Graph Generate EPS Postscript Figures: mlx | m | pdf | html
    • EPS vector graphics, avoid bitmap (jpg, png), use vector graphics.
    • m: figure(‘Renderer,’ ‘Painters’)

A.7 Tables links

A.7.1 Section 7.1 Basic Table Generation links

  1. Table Fill Data Row by Row or with Random Data: mlx | m | pdf | html
    • Generate an empty table and fill with data row by row.
    • Convert a random matrix to a table with column and row names defined with arrays.
    • m: table() + array2table() + strcat() + addvars() + matlab.lang.makeValidName()
  2. Order, Sort and Rename Columns: mlx | m | pdf | html
    • Convert a matrix to table with mean and sd columns. Rearrange and rename columns.
    • m: array2table() + rng() + addvars() + movevars() + removevars() + matlab.lang.makeValidName() + tb.Properties.VariableNames + tb.Properties.RowNames
  3. Array Based Row and Column Names: mlx | m | pdf | html
    • Generate a column and row named table. Convert row names to a column as strings. Remove Row Names.
    • Generate string-keys based on column names and values for a subset of columns.
    • m: array2table() + string() + strcat(‘rowA=,’ string((1:size(mt, 1)))) + tb_test_a.Properties.VariableNames + tb_test_a.Properties.RowNames + addvars(tb, rownames, ‘Before,’ 1) + strcat() + strjoin() + cellfun(@(x) f(x), ar_x)
  4. Select Subset of Rows and Columns: mlx | m | pdf | html
    • Conditional selection based on cell values and column and row names.
    • Select if row value matches any value from an arry of values.
    • Load a excel file and select based on column name string conditions.
    • m: tb(strcmp(tb.v1, “b”),:) + tb(tb.va==0.4,:) + readtable() + startsWith() + endsWith() + contains() + matches()

A.7.2 Section 7.2 Table Joining links

  1. Join Table by Keys: mlx | m | pdf | html
    • Left join larger table with smaller table with common keys.
    • m: join() + table()
  2. Stack Matlab Tables: mlx | m | pdf | html
    • Append columns to existing table. Stack tables vertically and horizontally.
    • m: array2table() + [tb_a tb_b] + [tb_a; tb_b] + tb.Properties.VariableNames + tb.Properties.RowNames
  3. Stack and Join Estimation and Simulation Results: mlx | m | pdf | html
    • Stack different tables together with varying columns via outerjoin, store with parallel parfor.
    • Stack different estiamtion results together into a common table where columns show parameter names and other strings.
    • Simulate a model, column combine simulation parameters with multi-row simulation results. Then row stack results from multiple simulations together.
    • m: array2table() + outerjoin() + addvars()
    • parallel: parfor

A.7.3 Section 7.3 Summarize links

  1. Table Summarize and Aggregate by Groups: mlx | m | pdf | html
    • Group table by one or several group variables, and aggregate over another numerical variable within group.
    • m: groupsummary() + table() + movevars()
Hai, Rong, and James J. Heckman. 2017. “Inequality in Human Capital and Endogenous Credit Constraints.” Review of Economic Dynamics 25: 4–36. https://doi.org/https://doi.org/10.1016/j.red.2017.01.001.
The MathWorks Inc. 2019. MATLAB. https://www.mathworks.com/products/matlab.html.
Wang, Fan. 2020. MEconTools: Tools for Analyzing Matlab Data Structures and Dynamic Programming. https://fanwangecon.github.io/MEconTools/.
Xie, Yihui. 2020. Bookdown: Authoring Books and Technical Documents with r Markdown. https://CRAN.R-project.org/package=bookdown.