Arrays are unique variables that store multiple values in a single variable name. In this lesson, we will discuss some particular types of arrays.

What Is Matlab Arrays?

In MATLAB®, all variables are arrays, including scalars and structs. No matter what type of data you want, you will store it in an array.

An array is a collection of elements with the same data type. A vector is a one-dimensional array, and a matrix is a two-dimensional array. A multidimensional array is an array whose dimensionality exceeds two dimensions (i.e., 3 or 4).

Professional Certificate Program In Data Science

The Ultimate Ticket to Top Data Science Job RolesExplore Course
Professional Certificate Program In Data Science

Array Creation in MATLAB

In MATLAB, we can create arrays in multiple ways.

The first way is to use spaces between elements:

>> A = [1 2 3 4 5 6 7 8 9 10 11 12]

It creates an array variable 'A' with one row and four columns. The 'A' variable is stored in the workspace, and the terminal will display the output in the command window as:

A = 1 2 3 4 5 6 7 8 9 10 11 12

The second way is to use commas in between elements: 

>> a = [1,2,3,4,5,6,7,8,9,10,11,12]

It creates an array variable 'a' having one row and four columns. The 'a' variable is stored in the workspace, and the terminal will display the output in the command window as

A = 1 2 3 4 5 6 7 8 9 10 11 12

Array Operations in MATLAB

In MATLAB, two categories of operations are available between arrays: array operations and matrix operations.

"Array operations" are implemented on corresponding elements in the two arrays. In other words, they are element-by-element operations.

"Matrix operations," on the other hand, are not implemented on corresponding elements in the two arrays. Instead, they are performed between two entire matrices.

Operation

Syntax

Array Addition

a+b

Array Subtraction

a-b

Array Multiplication

a .* b

Matrix Multiplication

a*b

Array Right Division

a ./ b

Array Left Division

a .\ b

Matrix Right Division

a/b

Matrix Left Division

a\b

Array Exponentiation

a .^ b

Data Scientist Master's Program

In Collaboration with IBMExplore Course
Data Scientist Master's Program

Indexing Arrays

In MATLAB®, every variable is an array that can hold many numbers. When you want to access selected elements of an array, use indexing.

Indexing is the process of selecting an element in an array based on its position in the array. The first element has 2 positions ie column index number and rows index number. 

For example:

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 the row and column subscripts, such as

A(3,1)

Here 3 is the row number and 1 is the column number

Hence the number selected will be - 9

Another way of doing it is specifying the product of column index and row index

A(3)

Special Arrays in MATLAB

We have a lot of functions that create arrays.

The ones() function creates an array of all ones.

ones(3,3)

>>

      1     1     1

      1     1     1

      1     1     1

The eye() function creates an identity matrix.

eye(4,4)

>>

      1     0     0     0

      0     1     0     0

      0     0     1     0

      0     0     0     1

The zeros() function creates an array of all zeros.

zeros(3)

>>

      0     0     0

      0     0     0    

      0     0     0    

The rand() function creates an array of uniformly distributed random numbers on (0,1).

rand(3,5)

>>

   0.8147    0.9134    0.2785    0.9649    0.9572

   0.9058    0.6324    0.5469    0.1576    0.4854

   0.1270    0.0975    0.9575    0.9706    0.8003

Learn From The Best in The Data Science Business!

Caltech Data Science BootcampExplore Course
Learn From The Best in The Data Science Business!

A Magic Square

Magic squares are one of the most fascinating mathematical objects. They're basically a way to make all numbers add up to the same number, no matter how you add them up. You can add them up row-wise, column-wise, or diagonally, and it'll always come out the same!

Magic() allows you to create your own magic square in just a few lines of code. It takes a singular argument that gives the size of the square. The argument must be a scalar greater than or equal to 3.

magic(4)

>>

   16     2     3    13

   5    11    10     8

   9     7     6    12

   4    14    15     1

Multidimensional Arrays

Multidimensional arrays in MATLAB are an extension of the normal two-dimensional matrix. Generally to generate a multidimensional array, we first create a two-dimensional array and extend it. To create a multidimensional array, we use the colon operator within the brackets [] to separate each dimension.

For example:

Array = [1 2 3; 4 5 6]

The above code creates a 2 x 3 matrix containing values 1-6. The following code generates a 3 x 4 matrix containing values 1-8.

1 2 3 

4 5 6 

Array Functions

  • length: Length of vector or largest array dimension
  • ndims: Number of array dimensions
  • numel: Number of array elements
  • size: Array dimensions
  • iscolumn: Determines whether the input is a column vector
  • isempty: Determines whether the array is empty
  • ismatrix: Determines whether the input is a matrix
  • isrow: Determines whether the input is a row vector
  • isscalar: Determines whether the input is scalar
  • isvector: Determines whether the input is a vector
  • blkdiag: Constructs block diagonal matrix from input arguments
  • circshift: Shifts array circularly
  • ctranspose: Complex conjugate transpose
  • diag: Diagonal matrices and diagonals of the matrix
  • flipdim: Flips array along the specified dimension
  • fliplr: Flips matrix from left to right
  • flipud: Flips matrix up to down
  • ipermute: Inverses permute dimensions of N-D array.
  • permute: Rearranges dimensions of N-D array.
  • repmat: Replicates and tile array.
  • reshape: Reshapes array.
  • rot90: Rotates matrix 90 degrees.
  • shiftdim: Shifts dimensions.
  • Sort: Sorts array elements in ascending or descending order
  • Sortrows: Sorts rows in ascending order
  • Squeeze: Removes singleton dimensions
  • Transpose: Transpose
  • Vectorize: Vectorizes expression

Matlab Array Examples

Array creation

a = [1 2 3 4]

a = 1×4

     1     2     3     4

Matrix transpose

a'

>>3×3

     1     2     7

     3     4     8

     5     6    10

Standard matrix multiplication

p = a*inv(a)

p = 3×3

    1.0000    0.0000   -0.0000

         0    1.0000   -0.0000

         0    0.0000    1.0000

Element wise multiplication

p=a.*a

p = 3×3

     1     9    25

     4    16    36

    49    64   100

Raise element to 3rd power

a.^3

>> 3×3

           1          27         125

           8          64         216

         343         512        1000

Become a Data Scientist With Real-World Experience

Data Scientist Master's ProgramExplore Course
Become a Data Scientist With Real-World Experience

FAQs

1. How do you write an array 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.

a = [1 2 3 4]

a = [1;2;3;4]

2. Are there arrays in MATLAB?

Yes. There are various types of arrays available in Matlab.

3. Do MATLAB arrays start at 0 or 1?

If you're a programmer, you've probably heard this before: in most programming languages, the first element of an array is element 0. In MATLAB, indexes start at 1.

What does this mean? If you're trying to access elements in your data using a loop, you'll need to remember that your index variable starts at 1—not 0!

4. What is a 2D array in MATLAB?

In MATLAB®, a multidimensional array is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns.

5. What is a 1D array in MATLAB?

In Matlab, an array is a row or a column, also known as a vector. An index is a positive integer that identifies the position of a value in the vector. To access a value in an array, use parentheses to enclose the index value.

Our Data Scientist Master's Program covers core topics such as R, Python, Machine Learning, Tableau, Hadoop, and Spark. Get started on your journey today!

Conclusion

If you want to become a data scientist, there's no better way to start than learning MATLAB.

MATLAB is one of the most sought-after programming languages in data science, and we've got the course for you!

Simplilearn's Data Scientist Masters Program will help you master MATLAB and other programming languages like Python and R. You'll also learn how to apply data science techniques to solve real-world problems.

The course covers various topics in depth and has a lot of practical components for getting you job-ready from day one.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors