1 Array Broadcast and Expansion Examples

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

Matrix broadcasting was added to matlab’s recent editions. This is an important step for vectorizing codes. Proper usage of broadcasting reduces memory allocation requirements for matrix matrix operations.

1.1 Broadcasting with A Row and a Column

Below we add together a 1 by 3 and 4 by 1 array, that should not work. With broadcasting, it is assumed that we will mesh the arrays and then sum up the meshed matrixes.

clear all
ar_A = [1,2,3];
ar_B = [4,3,2,1]';
disp(size(ar_A));

     1     3

disp(size(ar_B));

     4     1

mt_A_B_broadcast = ar_A + ar_B;
disp(mt_A_B_broadcast);

     5     6     7
     4     5     6
     3     4     5
     2     3     4

mt_A_B_broadcast_product = ar_A.*ar_B;
disp(mt_A_B_broadcast_product);

     4     8    12
     3     6     9
     2     4     6
     1     2     3

1.2 Broadcasting with One Row and One Matrix

Below we add together a 1 by 3 and 4 by 3 matrix, that should not work. With broadcasting, it is assumed that we will repeat the array four times, duplicating the single row four times, so the matrix dimensions match up.

clear all
ar_A = [1,2,3];
mt_B = [4,3,2,1;5,4,3,2;6,5,4,3]';
disp(size(ar_A));

     1     3

disp(size(mt_B));

     4     3

mt_A_B_broadcast = ar_A + mt_B;
disp(mt_A_B_broadcast);

     5     7     9
     4     6     8
     3     5     7
     2     4     6

mt_A_B_broadcast_product = ar_A.*mt_B;
disp(mt_A_B_broadcast_product);

     4    10    18
     3     8    15
     2     6    12
     1     4     9

1.3 Broadcasting with One Column and One Matrix

Below we add together a 4 by 1 and 4 by 3 matrix, that should not work. With broadcasting, it is assumed that we will repeat the column three times, duplicating the single column three times, so the matrix dimensions match up.

clear all
ar_A = [4,3,2,1]';
mt_B = [4,3,2,1;5,4,3,2;6,5,4,3]';
disp(size(ar_A));

     4     1

disp(size(mt_B));

     4     3

mt_A_B_broadcast = ar_A + mt_B;
disp(mt_A_B_broadcast);

     8     9    10
     6     7     8
     4     5     6
     2     3     4

mt_A_B_broadcast_product = ar_A.*mt_B;
disp(mt_A_B_broadcast_product);

    16    20    24
     9    12    15
     4     6     8
     1     2     3

1.4 Expand with Broadcast, Percentage Choice grids

clear all
ar_w_perc = [0.1,0.5,0.9]

ar_w_perc = 1x3    
    0.1000    0.5000    0.9000

ar_w_level = [-2,0,2]

ar_w_level = 1x3    
    -2     0     2

fl_b_bd = -4

fl_b_bd = -4

ar_k_max = ar_w_level - fl_b_bd

ar_k_max = 1x3    
     2     4     6

ar_ak_perc = [0.1,0.3,0.7,0.9]

ar_ak_perc = 1x4    
    0.1000    0.3000    0.7000    0.9000

mt_k = (ar_k_max'*ar_ak_perc)'

mt_k = 4x3    
    0.2000    0.4000    0.6000
    0.6000    1.2000    1.8000
    1.4000    2.8000    4.2000
    1.8000    3.6000    5.4000

mt_a = (ar_w_level - mt_k)

mt_a = 4x3    
   -2.2000   -0.4000    1.4000
   -2.6000   -1.2000    0.2000
   -3.4000   -2.8000   -2.2000
   -3.8000   -3.6000   -3.4000

1.5 Expand Matrix Twice

clear all
% Same as above
ar_w_level = [-2,-1,-0.1]

ar_w_level = 1x3    
   -2.0000   -1.0000   -0.1000

fl_b_bd = -4

fl_b_bd = -4

ar_k_max = ar_w_level - fl_b_bd

ar_k_max = 1x3    
    2.0000    3.0000    3.9000

ar_ak_perc = [0.001, 0.1,0.3,0.7,0.9, 0.999]

ar_ak_perc = 1x6    
    0.0010    0.1000    0.3000    0.7000    0.9000    0.9990

mt_k = (ar_k_max'*ar_ak_perc)'

mt_k = 6x3    
    0.0020    0.0030    0.0039
    0.2000    0.3000    0.3900
    0.6000    0.9000    1.1700
    1.4000    2.1000    2.7300
    1.8000    2.7000    3.5100
    1.9980    2.9970    3.8961

mt_a = (ar_w_level - mt_k)

mt_a = 6x3    
   -2.0020   -1.0030   -0.1039
   -2.2000   -1.3000   -0.4900
   -2.6000   -1.9000   -1.2700
   -3.4000   -3.1000   -2.8300
   -3.8000   -3.7000   -3.6100
   -3.9980   -3.9970   -3.9961


% fraction of borrowing for bridge loan
ar_coh_bridge_perc = [0, 0.5, 0.999];

% Expand matrix to include coh percentage dimension
mt_k = repmat(mt_k, [1, length(ar_coh_bridge_perc)])

mt_k = 6x9    
    0.0020    0.0030    0.0039    0.0020    0.0030    0.0039    0.0020    0.0030    0.0039
    0.2000    0.3000    0.3900    0.2000    0.3000    0.3900    0.2000    0.3000    0.3900
    0.6000    0.9000    1.1700    0.6000    0.9000    1.1700    0.6000    0.9000    1.1700
    1.4000    2.1000    2.7300    1.4000    2.1000    2.7300    1.4000    2.1000    2.7300
    1.8000    2.7000    3.5100    1.8000    2.7000    3.5100    1.8000    2.7000    3.5100
    1.9980    2.9970    3.8961    1.9980    2.9970    3.8961    1.9980    2.9970    3.8961

mt_a = repmat(mt_a, [1, length(ar_coh_bridge_perc)])

mt_a = 6x9    
   -2.0020   -1.0030   -0.1039   -2.0020   -1.0030   -0.1039   -2.0020   -1.0030   -0.1039
   -2.2000   -1.3000   -0.4900   -2.2000   -1.3000   -0.4900   -2.2000   -1.3000   -0.4900
   -2.6000   -1.9000   -1.2700   -2.6000   -1.9000   -1.2700   -2.6000   -1.9000   -1.2700
   -3.4000   -3.1000   -2.8300   -3.4000   -3.1000   -2.8300   -3.4000   -3.1000   -2.8300
   -3.8000   -3.7000   -3.6100   -3.8000   -3.7000   -3.6100   -3.8000   -3.7000   -3.6100
   -3.9980   -3.9970   -3.9961   -3.9980   -3.9970   -3.9961   -3.9980   -3.9970   -3.9961

mt_a = mt_a

mt_a = 6x9    
   -2.0020   -1.0030   -0.1039   -2.0020   -1.0030   -0.1039   -2.0020   -1.0030   -0.1039
   -2.2000   -1.3000   -0.4900   -2.2000   -1.3000   -0.4900   -2.2000   -1.3000   -0.4900
   -2.6000   -1.9000   -1.2700   -2.6000   -1.9000   -1.2700   -2.6000   -1.9000   -1.2700
   -3.4000   -3.1000   -2.8300   -3.4000   -3.1000   -2.8300   -3.4000   -3.1000   -2.8300
   -3.8000   -3.7000   -3.6100   -3.8000   -3.7000   -3.6100   -3.8000   -3.7000   -3.6100
   -3.9980   -3.9970   -3.9961   -3.9980   -3.9970   -3.9961   -3.9980   -3.9970   -3.9961


% bridge loan component of borrowing
ar_brdige_a = (ar_coh_bridge_perc'*ar_w_level)'

ar_brdige_a = 3x3    
         0   -1.0000   -1.9980
         0   -0.5000   -0.9990
         0   -0.0500   -0.0999

ar_brdige_a = ar_brdige_a(:)'

ar_brdige_a = 1x9    
         0         0         0   -1.0000   -0.5000   -0.0500   -1.9980   -0.9990   -0.0999


% borrowing choices excluding bridge loan
mt_a_nobridge = mt_a - ar_brdige_a

mt_a_nobridge = 6x9    
   -2.0020   -1.0030   -0.1039   -1.0020   -0.5030   -0.0539   -0.0040   -0.0040   -0.0040
   -2.2000   -1.3000   -0.4900   -1.2000   -0.8000   -0.4400   -0.2020   -0.3010   -0.3901
   -2.6000   -1.9000   -1.2700   -1.6000   -1.4000   -1.2200   -0.6020   -0.9010   -1.1701
   -3.4000   -3.1000   -2.8300   -2.4000   -2.6000   -2.7800   -1.4020   -2.1010   -2.7301
   -3.8000   -3.7000   -3.6100   -2.8000   -3.2000   -3.5600   -1.8020   -2.7010   -3.5101
   -3.9980   -3.9970   -3.9961   -2.9980   -3.4970   -3.9461   -2.0000   -2.9980   -3.8962