LibraryArrays and Matrices: Creation, Indexing, and Manipulation

Arrays and Matrices: Creation, Indexing, and Manipulation

Learn about Arrays and Matrices: Creation, Indexing, and Manipulation as part of MATLAB Programming for Engineering and Scientific Research

MATLAB Arrays and Matrices: Building Blocks for Engineering

In MATLAB, arrays and matrices are fundamental data structures used extensively in engineering and scientific research. They provide a powerful and efficient way to store and manipulate numerical data, enabling complex calculations and simulations. This module will guide you through the creation, indexing, and manipulation of these essential components.

Creating Arrays and Matrices

MATLAB offers several intuitive ways to create arrays and matrices. You can define them directly using square brackets

code
[]
, with elements separated by spaces or commas for rows, and semicolons
code
;
to denote new rows. Special functions like
code
zeros
,
code
ones
,
code
eye
, and
code
rand
are also available for creating matrices with specific properties.

What character is used in MATLAB to separate rows when creating a matrix directly?

A semicolon (;).

Indexing: Accessing Elements

Accessing specific elements or subsets of your arrays and matrices is crucial. MATLAB uses 1-based indexing, meaning the first element is at index 1. For matrices, you specify the row and column index within parentheses, separated by a comma (e.g.,

code
A(row, column)
). You can also use indexing to extract entire rows, columns, or sub-matrices.

MATLAB uses 1-based indexing for accessing array and matrix elements.

To get the element in the 3rd row and 2nd column of a matrix named myMatrix, you would use the command myMatrix(3, 2).

MATLAB's indexing system starts at 1. For a vector v, the first element is v(1). For a matrix M, M(i, j) refers to the element in the i-th row and j-th column. You can also use the colon : operator to select all elements of a row or column. For example, M(2, :) selects the entire second row, and M(:, 3) selects the entire third column. To extract a sub-matrix, you can specify ranges for both rows and columns, like M(1:2, 3:4).

Matrix Manipulation

MATLAB excels at matrix operations. Beyond basic arithmetic (addition, subtraction, element-wise multiplication), it supports powerful matrix operations like matrix multiplication (

code
*
), transpose (
code
.'
), inverse (
code
inv
), determinant (
code
det
), and solving linear systems. These operations are optimized for performance, making MATLAB ideal for numerical computations.

Consider a 2x2 matrix A and a 2x2 matrix B. Matrix multiplication (A * B) involves a specific process: each element in the resulting matrix is the dot product of a row from A and a column from B. For example, the element at position (1,1) in the result is the dot product of the first row of A and the first column of B. This operation is fundamental in linear algebra and widely used in transformations and solving systems of equations.

📚

Text-based content

Library pages focus on text content

OperationMATLAB Operator/FunctionDescription
Element-wise Multiplication.*Multiplies corresponding elements of two matrices.
Matrix Multiplication
Performs standard matrix multiplication.
Transpose.'Converts rows to columns and columns to rows.
Inverseinv()Calculates the multiplicative inverse of a square matrix.
Determinantdet()Computes the determinant of a square matrix.

Practical Applications

Understanding arrays and matrices in MATLAB is essential for various engineering disciplines. They are used for:

  • Solving systems of linear equations.
  • Performing transformations in computer graphics and image processing.
  • Analyzing data sets in statistics and machine learning.
  • Simulating physical systems in mechanical, electrical, and aerospace engineering.

Mastering matrix operations in MATLAB is a cornerstone for efficient numerical computation and problem-solving in engineering and science.

Learning Resources

MATLAB Documentation: Matrices and Arrays(documentation)

The official MathWorks documentation provides a comprehensive overview of matrix and array creation, indexing, and manipulation in MATLAB.

MATLAB Tutorial: Working with Matrices(video)

A video tutorial from Coursera that explains the basics of creating and manipulating matrices in MATLAB.

MATLAB Basics: Creating Arrays(video)

A YouTube video demonstrating how to create different types of arrays and matrices in MATLAB.

MATLAB Indexing and Slicing(video)

This video focuses specifically on the techniques for indexing and slicing arrays and matrices in MATLAB.

MATLAB Array Operations(blog)

An article from MathWorks explaining the power and efficiency of MATLAB's array operations.

Introduction to MATLAB for Engineering Students(tutorial)

A comprehensive tutorial covering MATLAB fundamentals, including a section on matrices and arrays.

MATLAB Indexing Explained(blog)

A clear explanation of MATLAB's indexing system with practical examples.

Matrix (mathematics)(wikipedia)

Wikipedia's article on matrices, providing the mathematical background and definitions relevant to MATLAB operations.

MATLAB Functions for Matrix Creation(documentation)

A reference page detailing MATLAB functions like zeros, ones, eye, and rand for matrix generation.

Solving Linear Systems in MATLAB(documentation)

Documentation on how to use MATLAB's matrix capabilities to solve systems of linear equations.