# Arrays in matlab

## Creating Matrices and Arrays

This example shows basic techniques for creating arrays and matrices using MATLAB. Matrices and arrays are the fundamental representation of information and data in MATLAB.

To create an array with multiple elements in a single row, separate the elements with either a comma ',' or a space. This type of array is called a row vector.

disp('Create an array with four elements in a single row:') disp('>> a = [1 2 3 4]') a = [1 2 3 4]
Create an array with four elements in a single row: >> a = [1 2 3 4] a = 1 2 3 4

To create an array with multiple elements in a single column, separate the elements with semicolons ';'. This type of array is called a column vector.

disp('Create an array with four elements in a single column:') disp('>> a = [1; 2; 3; 4]') a = [1; 2; 3; 4]
Create an array with four elements in a single column: >> a = [1; 2; 3; 4] a = 1 2 3 4

To create a matrix that has multiple rows, separate the rows with semicolons.

disp('Create a matrix with three rows and three columns:') disp('>> a = [1 2 3; 4 5 6; 7 8 9]') a = [1 2 3; 4 5 6; 7 8 9]
Create a matrix with three rows and three columns: >> a = [1 2 3; 4 5 6; 7 8 9] a = 1 2 3 4 5 6 7 8 9

To create an evenly spaced array, specify the start and end point by using the ':' operator.

disp('Create an array that starts at 1, ends at 9, with each element separated by 2:') disp('>> x = ') x =
Create an array that starts at 1, ends at 9, with each element separated by 2: >> x = x = 1 3 5 7 9

Another way to create a matrix is to use a function, such as ones, zeros or rand.

disp('Create a 1-by-5 matrix of 0''s:') disp('>> z = zeros(1, 5)') z = zeros(1, 5)
Create a 1-by-5 matrix of 0's: >> z = zeros(1, 5) z = 0 0 0 0 0

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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

Select web site

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.

Sours: https://www.mathworks.com/help/matlabmobile/ug/creating-matrices-and-arrays.html

## Multidimensional Arrays

A multidimensional array in MATLAB&#; is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns.

Each element is defined by two subscripts, the row index and the column index. Multidimensional arrays are an extension of 2-D matrices and use additional subscripts for indexing. A 3-D array, for example, uses three subscripts. The first two are just like a matrix, but the third dimension represents pages or sheets of elements.

### Creating Multidimensional Arrays

You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array.

A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3 1 2 3 4 5 6 7 8 9

Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension. The syntax uses a colon in the first and second dimensions to include all rows and all columns from the right-hand side of the assignment.

A(:,:,2) = [10 11 12; 13 14 15; 16 17 18]
A = A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 10 11 12 13 14 15 16 17 18

The function can be a useful tool for building multidimensional arrays. For example, create a new 3-D array by concatenating with a third page. The first argument indicates which dimension to concatenate along.

B = cat(3,A,[3 2 1; 0 9 8; 5 3 7])
B = B(:,:,1) = 1 2 3 4 5 6 7 8 9 B(:,:,2) = 10 11 12 13 14 15 16 17 18 B(:,:,3) = 3 2 1 0 9 8 5 3 7

Another way to quickly expand a multidimensional array is by assigning a single element to an entire page. For example, add a fourth page to that contains all zeros.

B = B(:,:,1) = 1 2 3 4 5 6 7 8 9 B(:,:,2) = 10 11 12 13 14 15 16 17 18 B(:,:,3) = 3 2 1 0 9 8 5 3 7 B(:,:,4) = 0 0 0 0 0 0 0 0 0

### Accessing Elements

To access elements in a multidimensional array, use integer subscripts just as you would for vectors and matrices. For example, find the 1,2,2 element of , which is in the first row, second column, and second page of .

A = A(:,:,1) = 1 2 3 4 5 6 7 8 9 A(:,:,2) = 10 11 12 13 14 15 16 17 18

Use the index vector in the second dimension to access only the first and last columns of each page of .

C = C(:,:,1) = 1 3 4 6 7 9 C(:,:,2) = 10 12 13 15 16 18

To find the second and third rows of each page, use the colon operator to create your index vector.

D = D(:,:,1) = 4 5 6 7 8 9 D(:,:,2) = 13 14 15 16 17 18

### Manipulating Arrays

Elements of multidimensional arrays can be moved around in many ways, similar to vectors and matrices. , , and are useful functions for rearranging elements. Consider a 3-D array with two pages.

Reshaping a multidimensional array can be useful for performing certain operations or visualizing the data. Use the function to rearrange the elements of the 3-D array into a 6-by-5 matrix.

A = [1 2 3 4 5; 9 0 6 3 7; 8 1 5 0 2]; A(:,:,2) = [9 7 8 5 2; 3 5 8 5 1; 6 9 4 3 3]; B = reshape(A,[6 5])
B = 6×5 1 3 5 7 5 9 6 7 5 5 8 5 2 9 3 2 4 9 8 2 0 3 3 8 1 1 0 6 4 3

operates columnwise, creating the new matrix by taking consecutive elements down each column of , starting with the first page then moving to the second page.

Permutations are used to rearrange the order of the dimensions of an array. Consider a 3-D array .

M(:,:,1) = [1 2 3; 4 5 6; 7 8 9]; M(:,:,2) = [0 5 4; 2 7 6; 9 3 1]
M = M(:,:,1) = 1 2 3 4 5 6 7 8 9 M(:,:,2) = 0 5 4 2 7 6 9 3 1

Use the function to interchange row and column subscripts on each page by specifying the order of dimensions in the second argument. The original rows of are now columns, and the columns are now rows.

P1 = P1(:,:,1) = 1 4 7 2 5 8 3 6 9 P1(:,:,2) = 0 2 9 5 7 3 4 6 1

Similarly, interchange row and page subscripts of .

P2 = P2(:,:,1) = 1 2 3 0 5 4 P2(:,:,2) = 4 5 6 2 7 6 P2(:,:,3) = 7 8 9 9 3 1

When working with multidimensional arrays, you might encounter one that has an unnecessary dimension of length 1. The function performs another type of manipulation that eliminates dimensions of length 1. For example, use the function to create a 2-bybyby-4 array whose elements are each 5, and whose third dimension has length 1.

A = A(:,:,1,1) = 5 5 5 5 5 5 A(:,:,1,2) = 5 5 5 5 5 5 A(:,:,1,3) = 5 5 5 5 5 5 A(:,:,1,4) = 5 5 5 5 5 5

Use the function to remove the third dimension, resulting in a 3-D array.

B = B(:,:,1) = 5 5 5 5 5 5 B(:,:,2) = 5 5 5 5 5 5 B(:,:,3) = 5 5 5 5 5 5 B(:,:,4) = 5 5 5 5 5 5

### Related Topics

You have a modified version of this example. Do you want to open this example with your edits?

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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

Select web site

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.

Sours: https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html

## Matrices and Arrays

### Array Creation

To create an array with four elements in a single row, separate the elements with either a comma () or a space.

This type of array is a row vector.

To create a matrix that has multiple rows, separate the rows with semicolons.

a = [1 3 5; 2 4 6; 7 8 10]
a = 3×3 1 3 5 2 4 6 7 8 10

Another way to create a matrix is to use a function, such as , , or . For example, create a 5-by-1 column vector of zeros.

### Matrix and Array Operations

MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or function.

ans = 3×3 11 13 15 12 14 16 17 18 20
ans = 3×3

To transpose a matrix, use a single quote ():

ans = 3×3 1 2 7 3 4 8 5 6 10

You can perform standard matrix multiplication, which computes the inner products between rows and columns, using the operator. For example, confirm that a matrix times its inverse returns the identity matrix:

p = 3×3 0 0 0 0 0

Notice that is not a matrix of integer values. MATLAB stores numbers as floating-point values, and arithmetic operations are sensitive to small differences between the actual value and its floating-point representation. You can display more decimal digits using the command:

p = 3×3 0 0 0 0 0

Reset the display to the shorter format using

affects only the display of numbers, not the way MATLAB computes or saves them.

To perform element-wise multiplication rather than matrix multiplication, use the operator:

p = 3×3 1 9 25 4 16 36 49 64

The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of to the third power:

ans = 3×3 1 27 8 64

### Concatenation

Concatenation is the process of joining arrays to make larger ones. In fact, you made your first array by concatenating its individual elements. The pair of square brackets is the concatenation operator.

A = 3×6 1 3 5 1 3 5 2 4 6 2 4 6 7 8 10 7 8 10

Concatenating arrays next to one another using commas is called horizontal concatenation. Each array must have the same number of rows. Similarly, when the arrays have the same number of columns, you can concatenate vertically using semicolons.

A = 6×3 1 3 5 2 4 6 7 8 10 1 3 5 2 4 6 7 8 10

### Complex Numbers

Complex numbers have both real and imaginary parts, where the imaginary unit is the square root of .

To represent the imaginary part of complex numbers, use either or .

c = [3+4i, 4+3j; -i, 10j]
c = 2×2 complex + i + i - i +i
Sours: https://www.mathworks.com/help/matlab/learn_matlab/matrices-and-arrays.html

## Matrices and Arrays

Array creation, combining, reshaping, rearranging, and indexing

Matrices and arrays are the fundamental representation of information and data in MATLAB®. You can create common arrays and grids, combine existing arrays, manipulate an array's shape and content, and use indexing to access array elements. For an overview of matrix and array manipulation, watch Working with Arrays.

expand all

### Create and Combine Arrays

 Create array of all zeros Create array of all ones Uniformly distributed random numbers Logical 1 (true) Logical 0 (false) Identity matrix Create diagonal matrix or get diagonal elements of matrix Block diagonal matrix Concatenate arrays Concatenate arrays horizontally Concatenate arrays vertically Repeat copies of array elements Repeat copies of array

### Determine Size, Shape, and Order

 Length of largest array dimension Array size Number of array dimensions Number of array elements Determine whether input is scalar Determine if array is sorted Determine if matrix or table rows are sorted Determine whether input is vector Determine whether input is matrix Determine whether input is row vector Determine whether input is column vector Determine whether array is empty

### Indexing

 Vector creation, array subscripting, and -loop iteration Terminate block of code or indicate last array index Convert linear indices to subscripts Convert subscripts to linear indices

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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

Select web site

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.

Sours: https://www.mathworks.com/help/matlab/matrices-and-arrays.html

## MATLAB Programming/Arrays

### Introduction to Arrays[edit | edit source]

An array is the most fundamental data type in MATLAB. In MATLAB, as in many traditional languages, arrays are a collection of several values of the same type. The string and number data type formerly presented are particular cases of arrays.

A matrix is an array with two dimensions. Most arrays have the same data type; however, a cell array is an array with varying data types. If no data type is specified for numbers, then the default data type is the equivalent to the double precision floating point in the C programming language on the same architecture. For example on x86 and powerpc a double has 64 bits.

The following list are pages involving work with MATLAB arrays:

### Row and Column Arrays[edit | edit source]

A row array is created using comma separated values inside brackets:

>> array = [0, 1, 4, 5] array = 0 1 4 5

Sometimes commas are omitted for simple row arrays. Omitting commas is not recommended because in larger more complex arrays white-spaces can be ambiguous. Commas almost always indicate an array is horizontal.

A column array is created using semicolons to separate values:

>> column = [1; 2; 3] column = 1 2 3

### Declaring multi-dimensional arrays[edit | edit source]

A two dimensional array (or a matrix) is declared with commas separating columns, and semicolons separating rows:

>> matrix = [1, 2, 3; 4, 5, 6] matrix = 1 2 3 4 5 6

Simple matrix manipulation is the basis of many linear algebra computations. To use arrays of more than two dimensions, a matrix has to be extended using indexing.

When declaring arrays in MATLAB all rows and all columns need have same size. If not an error message will be generated:

>> matrix = [1, 2, 3; 4, 5] ??? Error using ==> vertcat All rows in the bracketed expression must have the same number of columns.

### Indexing Arrays[edit | edit source]

Arrays are indexed using integers. To return a single element in a simple array, use a single integer.

>> array = [0, 1, 4, 5]; >> array(3) ans = 4

To return a single element in a two dimensional array one number as a row index and the second as a column index.

>> matrix = [1, 2, 3; 4, 5, 6]; >> matrix(2,2) ans = 5

To return multiple elements in an array an array can be used as an index.

>> array = [0, 1, 4, 5]; >> array([1 3]) ans = 0 4

To return the last element of an array use (end).

>> array = [0, 1, 4, 5]; >> array(end) ans = 5

A range of indexes can be returned using a colon(:)

>> array = [0, 1, 4, 5]; >> array(2:end) ans = 1 4 5

### Properties of MATLAB arrays and matrices[edit | edit source]

Contrary to low level languages such as C, an array in MATLAB is a more high level type of data: it contains various information about its size, its data type, and so on.

>> array = [0,1,4,5]; >> length(array) ans = 4 >> class(array) ans = double

The number of rows and columns of the matrix can be known through the built-in size function. Following the standard mathematical convention, the first number is the number of rows and the second is the number of columns:

>> matrix = [1, 2, 3; 4, 5, 6]; >> size(matrix) ans = 2 3

The goal of MATLAB arrays is to have a type similar to mathematical vectors and matrices. As such, row and column arrays are not equivalent. Mono-dimensional arrays are actually a special case of multi-dimensional arrays, and the 'size' function can be used for them as well.

>> size(array) ans = 1 4

Row and column do not have the same size, so they are not equivalent:

>> size(column) ans = 3 1 >> size(row) ans = 1 3

### Why Use Arrays?[edit | edit source]

A major advantage of using arrays and matrices is that it lets you avoid using loops to perform the same operation on multiple elements of the array. For example, suppose you wanted to add 3 to each element of the array [1,2,3]. If MATLAB didn't use arrays you would have to do this using a FOR loop:

>> array = [1,2,3]; >> for ii = array(ii) = array(ii) + 3; >> end >> array array = [4,5,6]

Doing this is not efficient in MATLAB, and it will make your programs run very slowly. Instead, you can create another array of 3s and add the two arrays directly. MATLAB automatically separates the elements:

>> array = [1,2,3]; >> arrayofthrees = [3,3,3]; >> array = array + arrayofthrees array = [4,5,6];

If all you are doing is adding a constant, you can also omit the declaration of 'arrayofthrees', as MATLAB will assume that the constant will be added to all elements of the array. This is very useful, for example if you use an array with variable size:

>> array = [1,2,3]; >> array + 3 ans = [4,5,6]

The same rule applies to scalar multiplication.

See Introduction to array operations for more information on the operations MATLAB can perform on arrays.

Arrays are a fundamental principle of MATLAB, and almost everything in MATLAB is done through a massive use of arrays. To have a deeper explanation of arrays and their operations, see Arrays and matrices.

Sours: https://en.wikibooks.org/wiki/MATLAB_Programming/Arrays
Lecture 3: Matrices \u0026 Arrays - Exploring MATLAB

## Working with Arrays in MATLAB

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 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 , which creates an array of logical values the same size as A with true values displayed here as 1 whenever A is less than 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 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 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.

Sours: https://www.mathworks.com/videos/working-with-arrays-in-matlabhtml

## Array Indexing

Every variable in MATLAB&#; is an array that can hold many numbers. When you want to access selected elements of an array, use indexing.

For example, consider the 4-by-4 matrix :

A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A = 4×4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as

Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:

Using a single subscript to refer to a particular element in an array is called linear indexing.

If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error.

However, on the left side of an assignment statement, you can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers.

A = 4×5 1 2 3 4 0 5 6 7 8 0 9 10 11 12 0 13 14 15 16 17

To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form . For example, list the elements in the first three rows and the second column of :

The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of :

The colon operator also allows you to create an equally spaced vector of values using the more general form .

B = 1×11 0 10 20 30 40 50 60 70 80 90

If you omit the middle step, as in , MATLAB uses the default step value of .

Sours: https://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html

5645 5646 5647 5648 5649