Loren on the Art of MATLAB

Turn ideas into MATLAB

Indexing with Parentheses 3

Posted by Loren Shure,

I have talked about indexing a bunch of times in the past. Recently I have visited quite a few customers who still get tripped up a bit sometimes. So I thought I'd try again.

Contents

Arrays

What are arrays? In MATLAB, they are containers that hold information. And they are "regular". By that, I mean that they have a uniform layout, equal numbers of elements in each row, each column, each page, etc. as you march along the dimensions. How do you get information into them, and out from them?

Where People Trip

One of the common places I see people trip is accessing information from cell arrays (and sometimes similarly for structures).

Function Calls

Parentheses mean multiple things in MATLAB. For example, you enclose the input arguments to a function between parentheses, like this:

image(magic(7))
axis square

Numeric Arrays

Another use of parentheses is to create a subset of an array. Let's suppose we have a matrix (2-D) and we want to create a new matrix from some particular rows and columns.

Anumeric = magic(5)
Amine = Anumeric([1 4],[2 5])
Anumeric =
    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
Amine =
    24    15
    12     3

We now have the 2x2 matrix defined by rows 1 and 4 and columns 2 and 5 from the original matrix A.

Cell Arrays

Similarly, we create a new cell array from an existing one, but indexing with parentheses for the rows and columns we want.

Cellofstuff = {Amine, 'loren', 'shure'; 17, 51, 153}
Cmine = Cellofstuff([1 2],[1 3])
Cellofstuff = 
    [2x2 double]    'loren'    'shure'
    [        17]    [   51]    [  153]
Cmine = 
    [2x2 double]    'shure'
    [        17]    [  153]

Tables

And tables work similarly. First I'm going to load in data from a MAT-file, into a struct rather than into separate variables.

Structpatient = load('patients.mat');

Next convert it to a table.

Tpatient = struct2table(Structpatient);
whos T*
  Name            Size            Bytes  Class    Attributes

  Tmine           5x6              4641  table              
  Tpatient      100x10            59738  table              

The table Tpatient is longer than I want to display, but I can easily look at a piece of it. How? By indexing with parentheses again to create a smaller table.

Tmine = Tpatient(1:5, [1 2 5 6 8 end])
Tmine = 
     Gender      LastName     Smoker    Systolic    Height    SelfAssessedHealthStatus
    ________    __________    ______    ________    ______    ________________________
    'Male'      'Smith'       true      124         71        'Excellent'             
    'Male'      'Johnson'     false     109         69        'Fair'                  
    'Female'    'Williams'    false     125         64        'Good'                  
    'Female'    'Jones'       false     117         67        'Fair'                  
    'Female'    'Brown'       false     122         64        'Good'                  

Structures

I most commonly see user code with scalar structures, by which I mean a structure with fields that contain other MATLAB stuff. But, since this is MATLAB, structs can also be arrays themselves. I think about them as cell arrays with one 'extra' dimension of indices being names, the rest being the usual indexing.

Sdata(1).FirstName = 'Loren';
Sdata(1).LastName = 'Shure';
Sdata(1).Height = 150;
Sdata(2).FirstName = 'Miss';
Sdata(2).LastName = 'Piggy';
Sdata(2).Height = 45;
Sdata(3).FirstName = 'Big';
Sdata(3).LastName = 'Bird';
Sdata(3).Height = 275;

To create a struct with only Sesame Street characters, I use parentheses again.

Ssesame = Sdata(1,2:3)
Ssesame = 
1x2 struct array with fields:
    FirstName
    LastName
    Height

or equivalently

   Ssesame = Sdata(2:3)

but NOT

   Ssesame = Sdata(2:3,1)

Follow Up

I plan to follow this up with discussions of other sorts of indexing. Hope you find this helpful. You can let me know here.


Get the MATLAB code

Published with MATLAB® R2016a

200 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.