Arrays in MATLAB Get Certified
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);
Expected output:
1 2 3 4 5
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 zeros
zero_matrix = zeros(3, 3);
disp(zero_matrix);
Expected output:
1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 0
3D Arrays
3D arrays add a third dimension to data, allowing for stacks of matrices:
% Creating a 3D array with two slices
array3D(:,:,1) = [1, 2; 3, 4];
array3D(:,:,2) = [5, 6; 7, 8];
disp(array3D);
Expected output (two slices):
(:,:,1) =
1 2
3 4
(:,:,2) =
5 6
7 8
Array Operations
Basic Operations
Arrays support basic arithmetic operations such as addition and element-wise multiplication:
% Adding two arrays
A = [1, 2, 3];
B = [4, 5, 6];
C = A + B;
disp(C);
% Element-wise multiplication
D = A .* B;
disp(D);
Expected output:
5 7 9
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);
Expected output:
1 3 5
2 4 6
Array Concatenation
Concatenate arrays horizontally, vertically, or along the third dimension:
% 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);
Expected output:
1 2 3 4
1 2
3 4
(:,:,1) =
1 2
(:,:,2) =
3 4
Accessing Array Elements
MATLAB uses 1-based indexing. You can access single elements, rows, columns or sub-arrays.
Accessing Single Elements
% Accessing a single element in a 1D array
A = [10, 20, 30, 40, 50];
disp(A(3)); % 3rd element
% Accessing a single element in a 2D array
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
disp(B(2, 3)); % 2nd row, 3rd column
Expected output:
30
6
Accessing Rows and Columns
% Accessing an entire row
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
disp(B(2, :)); % All elements of the 2nd row
% Accessing an entire column
disp(B(:, 3)); % All elements of the 3rd column
Expected output:
4 5 6
3
6
9
Accessing Subarrays
% Accessing a subarray
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
disp(B(1:2, 2:3)); % Rows 1-2, Columns 2-3
Expected output:
2 3
5 6
Logical Indexing
% Logical indexing
C = [10, 20, 30, 40, 50];
indices = C > 25; % Elements greater than 25
disp(C(indices));
Expected output:
30 40 50
Linear Indexing
% Accessing using linear indices
D = [1, 2; 3, 4];
disp(D(3)); % 3rd element in column-major order
Expected output:
3
Modifying Array Elements
Modifying Single Elements
% Modifying a single element
A = [10, 20, 30, 40, 50];
A(3) = 35; % Change the 3rd element
disp(A);
Expected output:
10 20 35 40 50
Modifying Rows and 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);
Expected output:
1 2 3
10 11 12
7 8 9
1 2 13
10 11 14
7 8 15
Modifying Subarrays
% Modifying a subarray
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B(1:2, 2:3) = [16, 17; 18, 19];
disp(B);
Expected output:
1 16 17
4 18 19
7 8 9
Appending Elements
% Appending a new column
B = [1, 2, 3; 4, 5, 6; 7, 8, 9];
B(:, 4) = [20; 21; 22];
disp(B);
% Appending a new row
B(4, :) = [23, 24, 25, 26];
disp(B);
Expected output:
1 2 3 20
4 5 6 21
7 8 9 22
1 2 3 20
4 5 6 21
7 8 9 22
23 24 25 26
Deleting Elements
% Deleting a row
B = [1, 2, 3, 20; 4, 5, 6, 21; 7, 8, 9, 22; 23, 24, 25, 26];
B(2, :) = []; % Remove the 2nd row
disp(B);
% Deleting a column
B(:, 3) = []; % Remove the 3rd column
disp(B);
Expected output:
1 2 3 20
7 8 9 22
23 24 25 26
1 2 20
7 8 22
23 24 26
Finding Array Properties
% Finding the size and length of an array
A = [1, 2, 3; 4, 5, 6];
disp(size(A)); % Dimensions
disp(length(A)); % Length of largest dimension
Expected output:
2 3
3
Useful MATLAB Functions for Arrays
sort(N) sorts the elements of vector N in ascending order.find(N) returns the indices of the nonzero elements of N.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.