Academic Block

Logo of Academicblock.net

Arrays in MATLAB

Arrays are essential to MATLAB, forming the foundation for most data structures and operations. MATLAB supports arrays of various dimensions, including 1D, 2D, and 3D arrays. In this section, we’ll explore array creation, manipulation, common operations, and useful functions.

Creating Arrays

1D Arrays (Vectors)

1D arrays, also known as vectors, are a sequence of elements stored in a single row or column. They can be created using square brackets:

% Creating a row vector
row_vector = [1, 2, 3, 4, 5];
disp(row_vector);

% Creating a column vector
column_vector = [1; 2; 3; 4; 5];
disp(column_vector);
Output for row_vector:

    1   2   3   4   5

Output for column_vector:

    1
    2
    3
    4
    5
    

2D Arrays (Matrices)

2D arrays, or matrices, consist of rows and columns. Use semicolons to separate rows:

% Creating a 2D array (matrix)
matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];
disp(matrix);

% Creating a matrix of specific size filled with zeros
zero_matrix = zeros(3, 3);
disp(zero_matrix);
Output for matrix:

    1   2   3
    4   5   6
    7   8   9

Output for zero_matrix: 

    0   0   0
    0   0   0
    0   0   0
    

3D Arrays

3D arrays add a third dimension to data, allowing for stacks of matrices. Use the syntax array(:,:,index) to specify slices along the third dimension:

% Creating a 3D array with two slices
array3D(:,:,1) = [1, 2; 3, 4];
array3D(:,:,2) = [5, 6; 7, 8];
disp(array3D);
Output for array3D (Slice 1):

    1   2
    3   4

Output for array3D (Slice 2): 

    5   6
    7   8
    

Array Operations

Basic Operations

Arrays in MATLAB support basic arithmetic operations such as addition, subtraction, and element-wise operations:

% Adding two arrays
A = [1, 2, 3];
B = [4, 5, 6];
C = A + B;
disp(C);

% Element-wise multiplication
D = A .* B;
disp(D);
Output for C: 

    5   7   9

Output for D:  

    4   10   18
    

Reshaping Arrays

Use the reshape function to rearrange an array into a new shape:

% Reshaping a 1D array into a 2D array
A = [1, 2, 3, 4, 5, 6];
B = reshape(A, 2, 3);
disp(B);
Output: 

    1   3   5
    2   4   6
    

Array Concatenation

Concatenate arrays horizontally, vertically, or along the third dimension using [ ] or the cat function:

% Horizontal concatenation
A = [1, 2];
B = [3, 4];
C = [A, B];
disp(C);

% Vertical concatenation
D = [A; B];
disp(D);

% Concatenation along the third dimension
E = cat(3, A, B);
disp(E);
Output for C: 

    1   2   3   4

Output for D: 

    1   2
    3   4

Output for E (Slice 1): 

    1   2

Output for E (Slice 2):  

    3   4
    

Accessing Array Elements

MATLAB provides multiple ways to access elements in arrays. You can access single elements, rows, columns, or subarrays using indexing. MATLAB uses 1-based indexing, meaning indices start from 1.

Accessing Single Elements

Use parentheses () to access elements by their index:

% Accessing a single element in a 1D array
A = [10, 20, 30, 40, 50];
disp(A(3)); % Access the 3rd element

% Accessing a single element in a 2D array
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
disp(B(2, 3)); % Element in the 2nd row, 3rd column
Output for A(3):

    30

Output for B(2,3):

    6
    

Accessing Rows and Columns

Use the colon operator : to access entire rows or columns:

% Accessing an entire row
disp(B(2, :)); % All elements in the 2nd row

% Accessing an entire column
disp(B(:, 3)); % All elements in the 3rd column
Output for B(2,:):  

    4   5   6

Output for B(:,3): 

    3
    6
    9
    

Accessing Subarrays

You can specify a range of rows and columns to access subarrays:

% Accessing a subarray
disp(B(1:2, 2:3)); % Rows 1-2, Columns 2-3
Output:  

    2   3
    5   6
    

Logical Indexing

Logical arrays can be used to access elements based on a condition:

% Logical indexing
C = [10, 20, 30, 40, 50];
indices = C > 25; % Find elements greater than 25
disp(C(indices)); % Access elements where the condition is true
Output: 

    30   40   50
    

Linear Indexing

In MATLAB, arrays can also be accessed using a single linear index:

% Accessing using linear indices
D = [1, 2; 3, 4];
disp(D(3)); % Access the 3rd element in column-major order
Output:

    3
    

Modifying Array Elements

Array elements can be modified using indexing. You can change a single element, an entire row or column, or a subarray.

Modifying Single Elements

Use indexing to assign a new value to a specific element:

% Modifying a single element
A = [10, 20, 30, 40, 50];
A(3) = 35; % Change the 3rd element
disp(A);
Output: 

    10   20   35   40   50
    

Modifying Rows and Columns

You can assign new values to entire rows or columns:

% Modifying a row
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B(2, :) = [10, 11, 12]; % Replace the 2nd row
disp(B);

% Modifying a column
B(:, 3) = [13; 14; 15]; % Replace the 3rd column
disp(B);
Output after modifying row: 

    1    2    3
   10   11   12
    7    8    9

Output after modifying column: 

    1    2   13
   10   11   14
    7    8   15
    

Modifying Subarrays

Subarrays can also be modified by assigning values to a range of indices:

% Modifying a subarray
B(1:2, 2:3) = [16, 17; 18, 19];
disp(B);
Output: 

    1   16   17
   10   18   19
    7    8   15
    

Appending Elements

You can expand arrays by appending new elements, rows, or columns:

% Appending a new column
B(:, 4) = [20; 21; 22];
disp(B);

% Appending a new row
B(4, :) = [23, 24, 25, 26];
disp(B);
Output after appending column: 

    1   16   17   20
   10   18   19   21
    7    8   15   22

Output after appending row: 

    1   16   17   20
   10   18   19   21
    7    8   15   22
   23   24   25   26
    

Deleting Elements

Use empty brackets [] to delete elements, rows, or columns:

% Deleting a row
B(2, :) = []; % Remove the 2nd row
disp(B);

% Deleting a column
B(:, 3) = []; % Remove the 3rd column
disp(B);
Output after deleting row: 

    1   16   17   20
    7    8   15   22
   23   24   25   26

Output after deleting column: 

    1   16   20
    7    8   22
   23   24   26
    

Finding Array Properties

MATLAB provides functions to analyze arrays:

% Finding the size and length of an array
A = [1, 2, 3; 4, 5, 6];
disp(size(A)); % Dimensions of the array
disp(length(A)); % Length of the largest dimension
Output: 

    2   3
    3
    

Useful MATLAB Functions for Arrays

Function
Description
zeros
Creates an array of zeros.
ones
Creates an array of ones.
reshape
Changes the shape of an array.
size
Returns the dimensions of an array.
cat
Concatenates arrays along a specified dimension.
numel
Returns the number of elements in an array.
length
Returns the length of the largest dimension.
sort
sort(N) sorts the vector N elements in ascending order.
find
find(N) returns a vector containing the indices of of nonzero elements

Practice Questions

Test Yourself

1. Create a 3×3 matrix with random numbers and find its transpose.

2. Compute the determinant of a 2×2 matrix and check if it has an inverse.

3. Perform element-wise multiplication on two matrices.

4. Create a 1D array of numbers from 1 to 10. Replace all even numbers with their squares.

5. Create a 4×4 identity matrix and modify the first row to contain the values [2, 3, 4, 5].

6. Generate a 3×3 matrix of random integers between 1 and 20. Extract all elements greater than 10 using logical indexing.