Matlab_Logo2.png

MatrixLaboratory $\Rightarrow$ Matlab

is an interpreted programming language. Originally developed for linear algebra and engineering problems, but now with wide applicability and toolboxes for areas ranging from medicine, economics, and machine learning.

A good way to introduce yourself to a new language is by trying to solve a "non-trivial" problem; learning the tools and syntax necessary to solve the problem along the way. This motivates the syntax/tools in a "why" versus "what" way!

help@scc.bu.edu

jbevan@bu.edu

bgregor@bu.edu

In [180]:
format compact

Motivation: An Exploration of an Interesting Dataset

Github repository of data we will use

https://github.com/bu-rcs/bu-rcs.github.io/tree/main/Bootcamp/Data

Citation

University of Wisconsin Population Health Institute. County Health Rankings & Roadmaps 2019.www.countyhealthrankings.org.

Data Source

https://www.countyhealthrankings.org/explore-health-rankings/rankings-data-documentation

Example Problem

How do we go about doing this?

Pseudo-code:

Read-in data file
Format contents how we want
Pre-allocate "data" matrix (to be all zero?)
Loop through formatted data
    (Process data to extract features of interest, categorize, etc)
    1: Calculate state averages
    2: ?
    3: ?
end
Output interesting results
Generate visualizations/plots

How do we read in a csv?

When I first was making this tutorial I forgot, since I don't usually need to for my background. So I googled...

readcsv()
readmatrix()
readtable()
opts=detectImportOptions()
In [181]:
test2=readmatrix("NE_HealthData.csv")
test2 =
   1.0e+04 *
  Columns 1 through 7
    0.9000       NaN       NaN    0.0014    0.0013    0.0026    0.0020
    0.9001       NaN       NaN    0.0011    0.0010    0.0021    0.0018
    0.9003       NaN       NaN    0.0013    0.0011    0.0026    0.0022
    0.9005       NaN       NaN    0.0011    0.0012    0.0028    0.0021
    0.9007       NaN       NaN    0.0010    0.0011    0.0026    0.0019
    0.9009       NaN       NaN    0.0014    0.0013    0.0028    0.0022
    0.9011       NaN       NaN    0.0012    0.0012    0.0030    0.0021
    0.9013       NaN       NaN    0.0010    0.0011    0.0025    0.0019
    0.9015       NaN       NaN    0.0013    0.0014    0.0030    0.0023
    2.3000       NaN       NaN    0.0015    0.0017    0.0030    0.0022
    2.3001       NaN       NaN    0.0013    0.0016    0.0034    0.0025
    2.3003       NaN       NaN    0.0017    0.0019    0.0036    0.0031
    2.3005       NaN       NaN    0.0009    0.0011    0.0023    0.0016
    2.3007       NaN       NaN    0.0014    0.0016    0.0034    0.0023
    2.3009       NaN       NaN    0.0013    0.0014    0.0025    0.0018
    2.3011       NaN       NaN    0.0012    0.0016    0.0034    0.0023
    2.3013       NaN       NaN    0.0011    0.0014    0.0027    0.0019
    2.3015       NaN       NaN    0.0011    0.0015    0.0024    0.0019
    2.3017       NaN       NaN    0.0014    0.0018    0.0033    0.0026
    2.3019       NaN       NaN    0.0015    0.0017    0.0034    0.0024
    2.3021       NaN       NaN    0.0015    0.0019    0.0037    0.0025
    2.3023       NaN       NaN    0.0012    0.0014    0.0025    0.0019
    2.3025       NaN       NaN    0.0016    0.0019    0.0034    0.0026
    2.3027       NaN       NaN    0.0015    0.0018    0.0031    0.0021
    2.3029       NaN       NaN    0.0018    0.0018    0.0033    0.0027
    2.3031       NaN       NaN    0.0013    0.0014    0.0030    0.0022
    2.5000       NaN       NaN    0.0014    0.0014    0.0025    0.0021
    2.5001       NaN       NaN    0.0010    0.0012    0.0021    0.0018
    2.5003       NaN       NaN    0.0013    0.0014    0.0026    0.0020
    2.5005       NaN       NaN    0.0014    0.0015    0.0029    0.0027
    2.5007       NaN       NaN    0.0011    0.0012    0.0027    0.0018
    2.5009       NaN       NaN    0.0013    0.0012    0.0025    0.0021
    2.5011       NaN       NaN    0.0012    0.0014    0.0029    0.0022
    2.5013       NaN       NaN    0.0018    0.0015    0.0031    0.0026
    2.5015       NaN       NaN    0.0012    0.0013    0.0020    0.0017
    2.5017       NaN       NaN    0.0011    0.0010    0.0022    0.0019
    2.5019       NaN       NaN    0.0013    0.0014    0.0025    0.0019
    2.5021       NaN       NaN    0.0012    0.0012    0.0023    0.0020
    2.5023       NaN       NaN    0.0013    0.0015    0.0028    0.0025
    2.5025       NaN       NaN    0.0017    0.0014    0.0021    0.0019
    2.5027       NaN       NaN    0.0014    0.0014    0.0027    0.0023
    3.3000       NaN       NaN    0.0013    0.0016    0.0028    0.0021
    3.3001       NaN       NaN    0.0012    0.0014    0.0029    0.0023
    3.3003       NaN       NaN    0.0013    0.0015    0.0027    0.0022
    3.3005       NaN       NaN    0.0013    0.0017    0.0030    0.0019
    3.3007       NaN       NaN    0.0015    0.0017    0.0034    0.0026
    3.3009       NaN       NaN    0.0012    0.0013    0.0024    0.0019
    3.3011       NaN       NaN    0.0012    0.0014    0.0028    0.0022
    3.3013       NaN       NaN    0.0011    0.0014    0.0028    0.0020
    3.3015       NaN       NaN    0.0009    0.0012    0.0027    0.0019
    3.3017       NaN       NaN    0.0014    0.0017    0.0030    0.0024
    3.3019       NaN       NaN    0.0013    0.0016    0.0033    0.0027
    4.4000       NaN       NaN    0.0016    0.0015    0.0028    0.0024
    4.4001       NaN       NaN    0.0011    0.0012    0.0023    0.0020
    4.4003       NaN       NaN    0.0014    0.0014    0.0030    0.0024
    4.4005       NaN       NaN    0.0012    0.0013    0.0025    0.0019
    4.4007       NaN       NaN    0.0017    0.0014    0.0029    0.0027
    4.4009       NaN       NaN    0.0012    0.0012    0.0024    0.0019
    5.0000       NaN       NaN    0.0013    0.0016    0.0026    0.0019
    5.0001       NaN       NaN    0.0010    0.0013    0.0026    0.0020
    5.0003       NaN       NaN    0.0013    0.0015    0.0030    0.0020
    5.0005       NaN       NaN    0.0014    0.0015    0.0032    0.0024
    5.0007       NaN       NaN    0.0010    0.0011    0.0021    0.0014
    5.0009       NaN       NaN    0.0016    0.0016    0.0029    0.0027
    5.0011       NaN       NaN    0.0013    0.0017    0.0034    0.0026
    5.0013       NaN       NaN    0.0012    0.0013    0.0030    0.0021
    5.0015       NaN       NaN    0.0011    0.0015    0.0024    0.0019
    5.0017       NaN       NaN    0.0011    0.0014    0.0028    0.0021
    5.0019       NaN       NaN    0.0013    0.0017    0.0030    0.0026
    5.0021       NaN       NaN    0.0014    0.0015    0.0031    0.0021
    5.0023       NaN       NaN    0.0012    0.0015    0.0023    0.0017
    5.0025       NaN       NaN    0.0012    0.0015    0.0024    0.0018
    5.0027       NaN       NaN    0.0012    0.0013    0.0028    0.0019
  Columns 8 through 14
    0.0094    0.0017    0.0088    0.0069    0.0014    0.0018    0.0078
    0.0097    0.0019    0.0090    0.0071    0.0013    0.0021    0.0072
    0.0097    0.0016    0.0084    0.0069    0.0015    0.0017    0.0081
    0.0083    0.0018    0.0089    0.0068    0.0008    0.0015    0.0084
    0.0097    0.0019    0.0096    0.0076    0.0008    0.0014    0.0082
    0.0095    0.0019    0.0086    0.0066    0.0017    0.0020    0.0078
    0.0092    0.0018    0.0089    0.0066    0.0013    0.0015    0.0080
    0.0077    0.0019    0.0091    0.0075    0.0006    0.0013    0.0080
    0.0082    0.0017    0.0083    0.0062    0.0015    0.0016    0.0083
    0.0070    0.0020    0.0087    0.0068    0.0015    0.0015    0.0078
    0.0084    0.0020    0.0081    0.0067    0.0017    0.0015    0.0079
    0.0056    0.0015    0.0090    0.0065    0.0019    0.0014    0.0080
    0.0084    0.0023    0.0088    0.0078    0.0008    0.0016    0.0076
    0.0047    0.0020    0.0090    0.0064    0.0020    0.0013    0.0077
    0.0070    0.0021    0.0088    0.0064    0.0016    0.0016    0.0073
    0.0077    0.0019    0.0083    0.0066    0.0015    0.0014    0.0080
    0.0045    0.0021    0.0091    0.0061    0.0016    0.0016    0.0078
    0.0057    0.0018       NaN    0.0058    0.0017    0.0015    0.0076
    0.0041    0.0019    0.0084    0.0052    0.0026    0.0016    0.0081
    0.0063    0.0020    0.0088    0.0067    0.0017    0.0015    0.0079
    0.0048    0.0018       NaN    0.0053    0.0024    0.0014    0.0078
    0.0081    0.0021    0.0085    0.0066    0.0013    0.0014    0.0080
    0.0059    0.0018    0.0086    0.0057    0.0021    0.0016    0.0082
    0.0050    0.0019    0.0085    0.0065    0.0020    0.0015    0.0077
    0.0052    0.0019       NaN    0.0052    0.0027    0.0014    0.0073
    0.0078    0.0021    0.0088    0.0069    0.0010    0.0015    0.0081
    0.0094    0.0021    0.0088    0.0073    0.0012    0.0018    0.0070
    0.0092    0.0020    0.0092    0.0066    0.0010    0.0017    0.0080
    0.0085    0.0019    0.0088    0.0064    0.0016    0.0016    0.0077
    0.0095    0.0021    0.0088    0.0063    0.0014    0.0017    0.0081
    0.0088    0.0021    0.0092    0.0064    0.0012    0.0024    0.0072
    0.0096    0.0021    0.0089    0.0067    0.0013    0.0019    0.0074
    0.0079    0.0020    0.0082    0.0069    0.0014    0.0015    0.0077
    0.0095    0.0020    0.0085    0.0061    0.0026    0.0019    0.0082
    0.0085    0.0024    0.0094    0.0077    0.0009    0.0017    0.0071
    0.0098    0.0023    0.0094    0.0081    0.0008    0.0016    0.0067
    0.0095    0.0022    0.0086    0.0078    0.0006    0.0020    0.0073
    0.0094    0.0021    0.0093    0.0083    0.0006    0.0016    0.0068
    0.0087    0.0022    0.0091    0.0070    0.0007    0.0016    0.0080
    0.0100    0.0024    0.0071    0.0074    0.0024    0.0025    0.0041
    0.0092    0.0023    0.0091    0.0068    0.0011    0.0016    0.0081
    0.0088    0.0021    0.0089    0.0071    0.0010    0.0015    0.0081
    0.0087    0.0019    0.0088    0.0066    0.0012    0.0016    0.0081
    0.0081    0.0016    0.0094    0.0065    0.0014    0.0015    0.0081
    0.0075    0.0021    0.0089    0.0067    0.0013    0.0017    0.0078
    0.0074    0.0017    0.0092    0.0055    0.0021    0.0015    0.0077
    0.0090    0.0020    0.0095    0.0071    0.0013    0.0015    0.0075
    0.0092    0.0020    0.0088    0.0070    0.0010    0.0015    0.0081
    0.0093    0.0019    0.0088    0.0070    0.0009    0.0015    0.0081
    0.0088    0.0020    0.0093    0.0076    0.0007    0.0013    0.0084
    0.0089    0.0022    0.0086    0.0073    0.0011    0.0018    0.0079
    0.0078    0.0020    0.0084    0.0057    0.0015    0.0014    0.0081
    0.0095    0.0020    0.0084    0.0066    0.0018    0.0018    0.0080
    0.0096    0.0021    0.0093    0.0083    0.0007    0.0017    0.0080
    0.0094    0.0018    0.0092    0.0072    0.0010    0.0017    0.0086
    0.0089    0.0022    0.0091    0.0078    0.0011    0.0017    0.0076
    0.0096    0.0019    0.0082    0.0061    0.0023    0.0020    0.0078
    0.0092    0.0021    0.0093    0.0076    0.0010    0.0015    0.0084
    0.0076    0.0019    0.0089    0.0068    0.0012    0.0017    0.0076
    0.0072    0.0022    0.0094    0.0069    0.0010    0.0015    0.0074
    0.0100    0.0018    0.0082    0.0062    0.0016    0.0017    0.0076
    0.0060    0.0017    0.0081    0.0056    0.0015    0.0016    0.0077
    0.0087    0.0022    0.0090    0.0081    0.0008    0.0017    0.0073
    0.0044    0.0018       NaN    0.0050    0.0024    0.0014    0.0079
    0.0049    0.0021    0.0092    0.0062    0.0013    0.0015    0.0082
    0.0062    0.0021       NaN    0.0068    0.0012    0.0018    0.0077
    0.0068    0.0022    0.0088    0.0067    0.0013    0.0018    0.0077
    0.0052    0.0018    0.0089    0.0060    0.0013    0.0016    0.0076
    0.0051    0.0020    0.0089    0.0053    0.0017    0.0020    0.0076
    0.0085    0.0018    0.0090    0.0063    0.0014    0.0019    0.0081
    0.0083    0.0021    0.0090    0.0069    0.0012    0.0017    0.0074
    0.0070    0.0019    0.0085    0.0061    0.0016    0.0020    0.0072
    0.0082    0.0020    0.0088    0.0067    0.0011    0.0016    0.0078
  Column 15
    0.0034
    0.0035
    0.0029
    0.0040
    0.0040
    0.0033
    0.0029
    0.0043
    0.0039
    0.0033
    0.0033
    0.0019
    0.0030
    0.0037
    0.0034
    0.0032
    0.0018
    0.0034
    0.0043
    0.0026
    0.0032
    0.0033
    0.0034
    0.0039
    0.0027
    0.0042
    0.0043
    0.0027
    0.0024
    0.0039
    0.0003
    0.0043
    0.0036
    0.0028
    0.0035
    0.0049
    0.0003
    0.0052
    0.0047
    0.0050
    0.0044
    0.0039
    0.0037
    0.0035
    0.0028
    0.0022
    0.0030
    0.0040
    0.0038
    0.0046
    0.0037
    0.0035
    0.0033
    0.0041
    0.0036
    0.0031
    0.0030
    0.0040
    0.0031
    0.0037
    0.0021
    0.0030
    0.0025
    0.0038
    0.0040
    0.0063
    0.0039
    0.0043
    0.0029
    0.0026
    0.0033
    0.0028
    0.0032
In [182]:
opts=detectImportOptions("NE_HealthData.csv")
readmatrix("NE_HealthData.csv",opts)
opts = 
  DelimitedTextImportOptions with properties:
   Format Properties:
                    Delimiter: {','}
                   Whitespace: '\b\t '
                   LineEnding: {'\n'  '\r'  '\r\n'}
                 CommentStyle: {}
    ConsecutiveDelimitersRule: 'split'
        LeadingDelimitersRule: 'keep'
                EmptyLineRule: 'skip'
                     Encoding: 'US-ASCII'
   Replacement Properties:
                  MissingRule: 'fill'
              ImportErrorRule: 'fill'
             ExtraColumnsRule: 'addvars'
   Variable Import Properties: Set types by name using setvartype
                VariableNames: {'FIPS', 'State', 'County' ... and 12 more}
                VariableTypes: {'double', 'char', 'char' ... and 12 more}
        SelectedVariableNames: {'FIPS', 'State', 'County' ... and 12 more}
              VariableOptions: [1-by-15 matlab.io.VariableImportOptions] 
	Access VariableOptions sub-properties using setvaropts/getvaropts
        PreserveVariableNames: false
   Location Properties:
                    DataLines: [2 Inf]
            VariableNamesLine: 1
               RowNamesColumn: 0
            VariableUnitsLine: 0
     VariableDescriptionsLine: 0 
	To display a preview of the table, use preview
Error using matlab.io.ImportOptions/readmatrix (line 523)
Selected variables in the import options must have the same data type.
 To import heterogenous data, use readtable.

Data Types

Primitives: Integers/Floating Point/Characters/Strings/Booleans/etc.

Integers have some interesting behavior when you work with unsigned versions or exceed their range:

In [183]:
a=uint8(2)
b=uint8(30)
a =
  uint8
   2
b =
  uint8
   30
In [184]:
a-b
ans =
  uint8
   0
In [185]:
c=int8(100)
d=int8(50)
c+d
c =
  int8
   100
d =
  int8
   50
ans =
  int8
   127

Strings and characters sound the same, but can behave differently:

In [186]:
bp_char='atcg';
bp_string="atcg";
class(bp_char)
class(bp_string)
ans =
    'char'
ans =
    'string'
In [187]:
format long
a = double(2/3)
b = single(2/3)
eps(a)
eps(b)
format short
a =
   0.666666666666667
b =
  single
   0.6666667
ans =
     1.110223024625157e-16
ans =
  single
   5.9604645e-08

Sructs:

In [188]:
for i=1:10
    % What happens if we use bp_string here instead?
    patient(i).dna = bp_char(randi(4,1,20));
end
patient(2).dna
ans =
    'atgagctcatgccatccttg'
In [189]:
class(patient)
ans =
    'struct'

Maps:

In [190]:
mymap = containers.Map(["smallest prime","dull number","days in year"],[2,1729,365])
mymap = 
  Map with properties:
        Count: 3
      KeyType: char
    ValueType: double
In [191]:
mymap("days in year")
ans =
   365

readtable()

In [192]:
T = readtable("NE_HealthData.csv");
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'PreserveVariableNames' to true to use the original column headers as table variable names.
In [193]:
T(1,1:5)
ans =
  1x5 table
    FIPS         State         County    x_FairOrPoorHealth    x_Smokers
    ____    _______________    ______    __________________    _________
    9000    {'Connecticut'}    {'NA'}          13.801           12.731  
In [194]:
T(1:11,2)
ans =
  11x1 table
         State     
    _______________
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Maine'      }
    {'Maine'      }
In [195]:
T.State(1:11)
ans =
  11x1 cell array
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Connecticut'}
    {'Maine'      }
    {'Maine'      }
In [196]:
T(1,4)
ans =
  table
    x_FairOrPoorHealth
    __________________
          13.801      
In [197]:
T(1,4)+1
Error using eval
Undefined operator '+' for input arguments of type 'table'.

How do we index vectors/matrices (aka "learn to love colons"). A quick sidebar:

In [198]:
% Quick way to make a "test" matrix of any size
M = magic(5)
M =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
In [199]:
% Matlab is "column-major" and "one indexed"
disp(M(1,1))
disp(M(1,2))
disp(M(3,1))
disp(M(5,5))
    17
    24
     4
     9
In [200]:
% Slices
M(1,:)
M(2,:)
M(:,1)
% Ranges
M(1:3,2)
ans =
    17    24     1     8    15
ans =
    23     5     7    14    16
ans =
    17
    23
     4
    10
    11
ans =
    24
     5
     6

Back to working with our dataset:

In [201]:
size(T)
ans =
    73    15
In [202]:
T(1:10,4)
ans =
  10x1 table
    x_FairOrPoorHealth
    __________________
          13.801      
          10.596      
          13.044      
          10.679      
          10.377      
          14.443      
          11.546      
           10.47      
          13.385      
          14.991      

If we want to do any numerical operations on the columns of our data we need to put it into an appropriate numerical data type...

We can do this with table2array()

In [203]:
table2array(T(1:10,4))
ans =
   13.8007
   10.5960
   13.0442
   10.6788
   10.3772
   14.4427
   11.5463
   10.4704
   13.3852
   14.9915
In [204]:
T(1,1:5)
ans =
  1x5 table
    FIPS         State         County    x_FairOrPoorHealth    x_Smokers
    ____    _______________    ______    __________________    _________
    9000    {'Connecticut'}    {'NA'}          13.801           12.731  
In [205]:
T(1,6:8)
ans =
  1x3 table
    x_AdultsWithObesity    x_PhysicallyInactive    x_WithAccessToExerciseOpportunities
    ___________________    ____________________    ___________________________________
           25.8                    20.4                          94.019               
In [206]:
T(1,9:12)
ans =
  1x4 table
    x_ExcessiveDrinking    HighSchoolGraduationRate    x_SomeCollege    x_ChildrenInPoverty
    ___________________    ________________________    _____________    ___________________
          17.111               {'87.97176025'}            68.905               13.8        
In [207]:
T(1,13:15)
ans =
  1x3 table
    x_SevereHousingProblems    x_DriveAloneToWork    x_LongCommute_DrivesAlone
    _______________________    __________________    _________________________
            18.142                   78.169                    33.7           
In [208]:
T(1:20,10)
ans =
  20x1 table
    HighSchoolGraduationRate
    ________________________
        {'87.97176025' }    
        {'90.408640616'}    
        {'83.815090132'}    
        {'88.838187702'}    
        {'96.338567839'}    
        {'85.843052711'}    
        {'89.085431353'}    
        {'91.42347561' }    
        {'82.627573859'}    
        {'86.668977537'}    
        {'81.226332538'}    
        {'90.186062246'}    
        {'88.250684463'}    
        {'90.182926829'}    
        {'88.021582734'}    
        {'83.152173913'}    
        {'90.771461717'}    
        {'NA'          }    
        {'83.531347962'}    
        {'87.737435567'}    

How do we deal with missing values here?

In [ ]:

We'd like to be able to process on a state-by-state basis:

In [209]:
states=unique(T.State)
states =
  6x1 cell array
    {'Connecticut'  }
    {'Maine'        }
    {'Massachusetts'}
    {'New Hampshire'}
    {'Rhode Island' }
    {'Vermont'      }

How do we find a particular state in the table?

In [210]:
T(strcmp(T.State,"Connecticut"),1:5)
ans =
  9x5 table
    FIPS         State             County        x_FairOrPoorHealth    x_Smokers
    ____    _______________    ______________    __________________    _________
    9000    {'Connecticut'}    {'NA'        }          13.801           12.731  
    9001    {'Connecticut'}    {'Fairfield' }          10.596           10.158  
    9003    {'Connecticut'}    {'Hartford'  }          13.044           11.356  
    9005    {'Connecticut'}    {'Litchfield'}          10.679           12.063  
    9007    {'Connecticut'}    {'Middlesex' }          10.377           11.316  
    9009    {'Connecticut'}    {'New Haven' }          14.443           12.844  
    9011    {'Connecticut'}    {'New London'}          11.546           12.351  
    9013    {'Connecticut'}    {'Tolland'   }           10.47           10.543  
    9015    {'Connecticut'}    {'Windham'   }          13.385           14.357  

What happens if we might need to do this over and over many times? Is there any potential performance issue with this?

If so how can we mitigate it?

In [211]:
strcmp(T.State,states)
Error using strcmp
Inputs must be the same size or either one can be a scalar.
In [212]:
state_inds=cellfun(@(c)strcmp(c,T.State),states,'UniformOutput',false)
state_inds =
  6x1 cell array
    {73x1 logical}
    {73x1 logical}
    {73x1 logical}
    {73x1 logical}
    {73x1 logical}
    {73x1 logical}
In [213]:
state_inds2=cell2mat(reshape(state_inds,1,6))
state_inds2 =
  73x6 logical array
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   1   0   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   1   0   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   1   0   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   1   0   0
   0   0   0   0   1   0
   0   0   0   0   1   0
   0   0   0   0   1   0
   0   0   0   0   1   0
   0   0   0   0   1   0
   0   0   0   0   1   0
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
   0   0   0   0   0   1
In [214]:
mymap = containers.Map(states,state_inds)
mymap = 
  Map with properties:
        Count: 6
      KeyType: char
    ValueType: any
In [215]:
T(mymap("Connecticut"),{'State','County'})
ans =
  9x2 table
         State             County    
    _______________    ______________
    {'Connecticut'}    {'NA'        }
    {'Connecticut'}    {'Fairfield' }
    {'Connecticut'}    {'Hartford'  }
    {'Connecticut'}    {'Litchfield'}
    {'Connecticut'}    {'Middlesex' }
    {'Connecticut'}    {'New Haven' }
    {'Connecticut'}    {'New London'}
    {'Connecticut'}    {'Tolland'   }
    {'Connecticut'}    {'Windham'   }
In [216]:
table2array(T(mymap("Connecticut"),[4:9,11:15]))
ans =
  Columns 1 through 7
   13.8007   12.7310   25.8000   20.4000   94.0193   17.1107   68.9046
   10.5960   10.1579   21.3000   17.7000   96.8107   19.4012   70.8720
   13.0442   11.3562   26.2000   21.6000   96.8413   15.6600   69.4777
   10.6788   12.0631   27.7000   20.8000   82.9303   17.6350   68.0707
   10.3772   11.3164   25.5000   19.4000   96.7605   19.0825   76.1867
   14.4427   12.8440   28.1000   21.7000   95.4137   18.5541   65.8374
   11.5463   12.3508   29.6000   20.9000   92.0709   17.8461   66.4853
   10.4704   10.5435   24.9000   19.1000   76.6057   19.0502   75.3778
   13.3852   14.3570   30.3000   23.0000   81.8590   17.2326   62.0015
  Columns 8 through 11
   13.8000   18.1418   78.1694   33.7000
   12.7000   21.3150   72.2750   35.4000
   15.5000   16.8431   81.1092   29.4000
    8.1000   14.6960   83.8701   39.8000
    8.1000   14.0224   81.6107   40.5000
   16.7000   20.1326   78.1173   33.0000
   12.5000   15.0184   79.7801   28.6000
    6.4000   12.6065   79.9350   43.5000
   14.8000   15.8947   83.0190   38.5000
In [217]:
averages = zeros(numel(states),11);
it = 1;
for state = states
    averages(it,:) = mean(table2array(T(mymap(state),[4:9,11:15])))
    it = it + 1;
end
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
In [218]:
mymap.values()
ans =
  1x6 cell array
  Columns 1 through 4
    {73x1 logical}    {73x1 logical}    {73x1 logical}    {73x1 logical}
  Columns 5 through 6
    {73x1 logical}    {73x1 logical}