Skip to content
MathWorks - Mobile View
  • Inicie sesión cuenta de MathWorksInicie sesión cuenta de MathWorks
  • Access your MathWorks Account
    • Mi Cuenta
    • Mi perfil de la comunidad
    • Asociar Licencia
    • Cerrar sesión
  • Productos
  • Soluciones
  • Educación
  • Soporte
  • Comunidad
  • Eventos
  • Obtenga MATLAB
MathWorks
  • Productos
  • Soluciones
  • Educación
  • Soporte
  • Comunidad
  • Eventos
  • Obtenga MATLAB
  • Inicie sesión cuenta de MathWorksInicie sesión cuenta de MathWorks
  • Access your MathWorks Account
    • Mi Cuenta
    • Mi perfil de la comunidad
    • Asociar Licencia
    • Cerrar sesión

Vídeos y webinars

  • MathWorks
  • Vídeos
  • Vídeos-Inicio
  • Buscar
  • Vídeos-Inicio
  • Buscar
  • Comuníquese con ventas
  • Software de prueba
6:07 Video length is 6:07.
  • Description
  • Full Transcript
  • Related Resources

Working with Arrays in MATLAB

Create and manipulate MATLAB® arrays, including accessing elements using indexing.

MATLAB stores all types of data in arrays. This includes not only numeric data, but data of other types such as strings or even complex objects. So working with arrays is fundamental to working with MATLAB. With the MATLAB language, you can create arrays, access and assign values to array elements using a number of indexing methods, and perform many other operations to manipulate the array's contents.

Let's first look at creating arrays. You can create an array by specifying specific values using square brackets and commas or spaces to separate columns in a row such as A equals 1, 2, 3, 4 and semicolons to separate rows. You can create equally spaced one dimensional arrays with a column operator such as A equals 1 to 10, A equals 1 to 10 in steps of 2, or A equals 10 to 1 in steps of negative 2.

The linspace space function is similar to the colon operator, letting you specify a start and end value but gives control over the number of points such as 7. You can change the rows to columns with the transpose operator. You can also call a number of functions that generate elementary matrices with different contents such as ones, zeros, or random numbers. It can be more convenient to inspect the contents of an array by opening it into the variable editor.

Let's now look at how you can access and change the values of array elements with different forms of indexing. You can specify elements of an array by simple row and column indexing. Here is the element of A in the first row second column.

You can specify a range of rows and columns to access sections of an array such as row 1, columns 1 through 2. The elements do not have to be contiguous, such as row 1, columns 1 and 3. You can specify all rows or columns by using the colon operator, in this case specifying all columns. You can also use the end keyword such as row 1, columns 2 to the end or 2 the end minus 1.

You can assign values to specific elements by specifying indexing on the left hand side of the equation such as rows 1, columns 2 to the end minus 1 equals 10 10. You can delete one or more rows of an array such as rows 1 to 2, all the columns, by assigning them to the empty matrix denoted by square brackets. A is now two rows shorter.

Sometimes it is convenient to treat two dimensional arrays such as these as a one dimensional array as though all the columns were stacked together into a single column and specify single index. This is called linear indexing. For example, the element at row 1 column 2 can be accessed through one linear index, 5.

This is possible because MATLAB arrays are stored column wise in memory. In other words, each column in the array is stored one after another. So the element at row 1 column 2 is, in fact, the fifth element stored. The colon operator used on its own specifies all elements when using linear indexing, and it returns a single column vector with the entire array contents.

You can also access elements with what is known as logical indexing where you specify an indexing array of equal size filled with true or false values, like a mask. This is useful for operating on elements whose values match some criterion such as A is less than 0.5, which creates an array of logical values the same size as A with true values displayed here as 1 whenever A is less than 0.5. Note logical values like true and false are displayed as 1 and 0s respectively.

We can use logical arrays such as this to perform logical indexing such as set the elements of A where A is less than 0.5 to negative 1. To find the indices of array elements that match your criteria, use the Find function, which finds non-zero values together with a logical expression. This gives the linear indices of the elements that meet the condition A is less than 0.5. You can get the rows and column indices instead. You can see information about all indexing techniques in the documentation.

And finally, let's look at how you can extract some useful information about an array and perform some basic operations. You can get basic information on an array such as determine if it's empty, get the length-- usually used for 1d arrays-- size of all the dimensions, or the total number of elements. As we saw before, creating arrays with square brackets lets us concatenate a number of arrays together horizontally or vertically. Other useful array manipulation functions include flip left-right and flip up-down, repmat to replicate matrices, reshape, and sort.

Although the examples shown here use one and two dimensional arrays, most of these techniques can be applied to multi-dimensional arrays, as well. See the documentation for more information. This concludes the demonstration. Try these features in MATLAB now or watch one of the other videos.

Related Products

  • MATLAB

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

26:55
Working with Large Sets of Images in MATLAB Just Got Easier

Related Videos:

5:44
How to Preprocess Time Series Data with MATLAB
1:43
Setting the Initial Working Folder in MATLAB to be the Last...
2:31
Working with Dates and Times in MATLAB
6:06
What are Tables and How are They Used in MATLAB

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Comuníquese con ventas
  • Software de prueba

MathWorks

Accelerating the pace of engineering and science

MathWorks es el líder en el desarrollo de software de cálculo matemático para ingenieros

Descubra…

Explorar productos

  • MATLAB
  • Simulink
  • Software para estudiantes
  • Soporte para hardware
  • File Exchange

Probar o comprar

  • Descargas
  • Software de prueba
  • Comuníquese con ventas
  • Precios y licencias
  • Cómo comprar

Aprender a utilizar

  • Documentación
  • Tutoriales
  • Ejemplos
  • Vídeos y webinars
  • Formación

Obtener soporte

  • Ayuda para la instalación
  • MATLAB Answers
  • Consultoría
  • Centro de licencias
  • Contactar con soporte

Acerca de MathWorks

  • Ofertas de empleo
  • Sala de prensa
  • Misión social
  • Casos prácticos
  • Acerca de MathWorks
  • Select a Web Site United States
  • Centro de confianza
  • Marcas comerciales
  • Política de privacidad
  • Antipiratería
  • Estado de las aplicaciones

© 1994-2022 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Únase a la conversación