View on GitHub

Matlab Examples Matrix and Graphs

Matlab Code Examples

Star Fork Star DOI

This is a work-in-progress website consisting of Matlab example code for manipulating various data structures, dynamic programming, graphing and associated tasks.

bookdown site and bookdown pdf.

Materials gathered from various projects in which matlab is used. Matlab files are linked below by section with livescript files. Tested with Matlab 2019a. This is not a Matlab package, but a list of examples in PDF/HTML/Mlx formats. MEconTools is a package that can be installed with tools used in projects involving matlab code.

Bullet points in the Appendix show which matlab functions/commands are used to achieve various objectives. The goal of this repository is to make it easier to find/re-use codes produced for various projects. Some functions also rely on or correspond to functions from MEconTools.

From other repositories: For code examples, see Python Example Code, R Example Code, and Stata Example Code; For intro stat with R, see Intro Statistics for Undergraduates, and intro Math with Matlab, see Intro Mathematics for Economists. See here for all of Fan’s public repositories.

Please contact FanWangEcon for issues or problems.

1 Data Structures

1.1 Matrices and Arrays

  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
    • Divide an array into sub-segments.
    • Loop over numbers, find modulus (remainder) and quotient given divisor.
    • Check data and parameter types.
    • Compare approximately similar values.
    • m: imag() + isfloat() + iscell() + isnan() + isnumeric()

1.2 ND Dimensional Arrays

  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()

1.3 Cells

  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})

1.4 Characters and Strings

  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

1.5 Map Containers

  1. Container Map Basics: mlx | m | pdf | html
    • Numeric container map, dynamically filled container map.
    • Numeric scalar, string, matrix as values for map container.
    • String and stringed numeric arrays as keys for map container.
    • Get values for multiple keys in map.
    • m: isKey() + strjoin() + num2str() + 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]

1.6 Map Structure Array

  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

2 Functions and Programming

2.1 Development and Debugging

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

2.2 varargin Default Parameters

  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]

2.3 Dynamic Functions

  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()

3 Distributional Processes

3.1 Time Series

  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.
    • Given a monthly persistent AR(1) process, estimate the persistence of the process when data is aggregated at the annual and quadrennial levels, persistence weakens with aggregation.
    • m: normrnd() + for it_t=1:1:length(ar_shk) + plot(ar_t, ar_y) + polyfit(x, y, 1)
    • stats: fitlm(table, ‘y ~ x1 + x2’)
  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()

3.2 Cross-sectional Data

  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

4 Simulation

4.1 Normal Distribution

  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

5 Estimation

5.1 Linear Estimation

  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);

5.2 Nonlinear Estimation

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

6 Graphs

6.1 Figure Components

  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,:))

6.2 Basic Figure Types

  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)

6.3 Graph Functions

  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”);

6.4 Write and Read Plots

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

7 Tables

7.1 Basic Table Generation

  1. Table Generate Table And Fill Data Row by Row or Cell by Cell: mlx | m | pdf | html
    • Generate an empty table and fill with data row by row.
    • Replace or fill table cell value with tb.col{it_row} = rand() is faster than tb{it_row, st_col} = rand().
    • Convert a random matrix to a table with column and row names defined with arrays.
    • m: table() + array2table() + tb{it_row, st_col} = rand() + tb.col{it_row} = rand() + strcat() + addvars() + matlab.lang.makeValidName() + timeit + tic + toc
  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()

7.2 Table Joining

  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

7.3 Summarize

  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()

Please contact for issues or problems.

DOI

RepoSize CodeSize Language Release License