Lists and Indexing edit page

Almost every MTEX variable is a list of objects of one kind. An EBSD variable is a list of measurements, an grain2d variable is a list of grains, and grains.boundary is a list of grain boundary segments. An orientation variable commonly stores a list of orientations.

There is no separate type for one grain. A single grain is an grain2d list with length one.

Two consequences account for most list operations in MTEX.

  • An elementwise function or property acts on every list element. It usually returns one result per element: grains.area gives one area per grain, and ori.angle gives one angle per orientation. Reduction functions such as mean are exceptions because they combine several elements into one result. Loops are therefore almost never needed for elementwise calculations.
  • Ordinary parentheses indexing selects elements as it does for a MATLAB array. The result is a list of the same kind, so an operation that accepts the complete list also accepts the selection.

The diagram compares the two main forms of indexing. Position indices pick specified list entries in the requested order. A logical condition keeps the entries whose mask value is true.

The sections that follow show the indexing itself on plain numbers, for clarity. The same two forms then apply unchanged to any MTEX list.

Make a list

Square brackets create a list by placing values next to one another.

x = [1,2,3,4,5,6]
x =
     1     2     3     4     5     6

The colon operator is shorthand for a regularly spaced range. With no step specified, it uses a step of one.

x = 1:10
x =
     1     2     3     4     5     6     7     8     9    10

Apply an operation to every element

MATLAB applies many arithmetic operations elementwise. The dot in .^ means that every element of x is squared independently, rather than computing a matrix power.

y = x.^2
y =
     1     4     9    16    25    36    49    64    81   100

Select by position

A list of positions selects those elements in the given order. Here, ind selects the first, third, and fifth squared values.

ind = [1,3,5]
y(ind)
ind =
     1     3     5
ans =
     1     9    25

Select by condition

A condition evaluated for the complete list returns one logical value per element. A logical value is either true or false. It can be used as an index to retain only the corresponding true entries.

cond = iseven(x)
y(cond)
cond =
  1×10 logical array
   0   1   0   1   0   1   0   1   0   1
ans =
     4    16    36    64   100

The condition tests which values in x are even. Using the same mask to index y returns their squares. This works because x and y have the same length and order.

Use the same pattern with MTEX objects

Nearly every MTEX selection has the same form. For example, grains(grains.area > 100) selects grains with area greater than 100, ebsd(ebsd.mad < 1) selects measurements with a mean angular deviation below 1, and gB(gB.misorientation.angle > 10*degree) selects grain boundary segments with a misorientation angle greater than 10 degrees.

In each expression, MTEX computes one property value per object. The comparison turns those values into a logical mask, and parentheses apply the mask to the original list.

MTEX also provides two domain-specific selectors. A phase name selects all measurements of that phase, as in ebsd('Forsterite'). For spatial data, a position selects the object found there, as in grains(x,y). See Selecting Grains for spatial grain selections.

References

  • MathWorks, Array Indexing, MATLAB documentation. It defines positional and logical indexing and describes the array rules used on this page.

Next

Continue with Configuration to learn how session preferences control display, computation, and plotting defaults.

Citing this page. This page is part of the documentation of MTEX, a free and open source MATLAB toolbox for analyzing and modeling crystallographic textures. It was written by The MTEX Developers and is published at https://mtex-toolbox.github.io/ListsAndIndexing.html. If you use MTEX, or reuse text or figures from this page, in your research, please cite

F. Bachmann, R. Hielscher, H. Schaeben: Texture Analysis with MTEX - Free and Open Source Software Toolbox, Solid State Phenomena 160 (2010), 63-68. 10.4028/www.scientific.net/SSP.160.63

BibTeX
@article{bachmann2010mtex,
  author  = {F. Bachmann and R. Hielscher and H. Schaeben},
  title   = {Texture Analysis with MTEX - Free and Open Source Software Toolbox},
  journal = {Solid State Phenomena},
  volume  = {160},
  pages   = {63-68},
  year    = {2010},
  doi     = {10.4028/www.scientific.net/SSP.160.63},
  url     = {https://doi.org/10.4028/www.scientific.net/SSP.160.63}
}

Other papers describing specific MTEX methods are listed under Publications — please cite the one that best fits your application. The MTEX source code is licensed under the GNU General Public License v2.0; the text and figures of this documentation are licensed under CC BY 4.0, which permits reuse — including by automated systems — provided The MTEX Developers and this page are credited.