MATLAB stands for Matrix Laboratory, which is an environment based on matrix operations. All data in MATLAB is stored in the form of matrices or multidimensional arrays. Vectors and scalars are two special forms of matrices.
A vector refers to a matrix that is either a single row or a single column, and it is the foundation of matrices. To become proficient in MATLAB, one of the fundamental skills is to be able to manipulate vectors skillfully, as vector operations can enable vectorization of certain calculations.
In many programming scenarios, vectorized code can execute over 10 times faster than usual. Therefore, vectorization can be said to be one of the most common and effective means of writing efficient MATLAB code.
1. Vector Assignment
Assignment means giving a value to an identifier that represents a constant or a variable. Variables or constants in MATLAB represent matrices, scalars can be viewed as 1×1 matrices, and vectors can be seen as 1×N (row vector) or N×1 matrices. The general form of an assignment statement is “variable = expression (or number)”.
There are several ways to assign values to vectors:
(1) Assign values to vectors using the vector construction operator (square brackets [ ]) and numeric values.
【Example 1】 Assigning a vector with 9 elements from 1 to 9 to the variable x in the MATLAB command window
>> x = [1,2,3,4,5,6,7,8,9]
x =
1 2 3 4 5 6 7 8 9
The command window is the main interface for user interaction with MATLAB . “>>” is the prompt where various MATLAB commands can be typed to produce corresponding results.
In Example 1, the obtained x is a row vector, which is placed in square brackets [] with values separated by commas or spaces. To obtain a column vector, values should be separated by semicolons. The transpose operator “’” can be used to convert between row and column vectors.
【Example 2】 Generate a column vector and assign it to y and z.
>> y=[1;2;3]
y =
1 2 3
>> z=[1,2,3]'
z =
1 2 3
(2) Assign values to vectors using colon expressions.
MATLAB defines a unique colon expression for assigning values to row vectors, with the basic usage format as follows:
j:k equivalent to [j,j+1,...,k] j:k is empty, if j>k j:i:k equivalent to [j,j+i,j+2i,...,k] j:i:k is empty, if i == 0, if i>0 and j>k, or if i<0 and j<k
【Example 3】 Generate a row vector using the colon expression.
>> D = 1:4
D =
1 2 3 4
>> E = 0:.1:.5
E =
0 0.1000 0.2000 0.3000 0.4000 0.5000
(3) Assign values to vectors using functions.
Functions such as linspace(), logspace(), zeros(), ones(), rand(), randn(), etc., can be used to generate vectors.
【Example 4】 Generate a row vector using the linspace() function.
>> linspace(1,4,5)
ans =
1.0000 1.7500 2.5000 3.2500 4.0000
(4) Use a for loop to assign values to vectors.
【Example 5】 Assign values to vector b using a for loop.
>> for m=1:5, b(m)=m^2;end >> b
b =
1 4 9 16 25
2. Vector Operations
Vector operations can be performed on a specific element or several elements within a vector, or they can be conducted as element-wise operations on the entire vector.
Performing operations on specific elements of a vector is relatively simple, requiring only the use of parentheses () along with the element’s index in the vector. This can be used to access element values, assign values to elements, or modify their values. It is worth noting that using end as an index allows convenient operations on the last element of the vector, while (end-1), (end-2), etc., can be used for the second-to-last and third-to-last elements.
【Example 6】 Swap the 3rd element and the last element of vector b from Example 5 and display the result.
>> c=b(3);b(3)=b(end);b(end)=c; >> b
b =
1 4 25 16 9
During the process of accessing element values in a vector, if the index in parentheses exceeds the number of elements in the vector, an error will occur. However, when assigning values to vector elements, if the index exceeds the number of elements, the assignment will be completed with zero padding. For specific situations, please refer to Example 7.
【Example 7】 The situation that occurs when the index of an element is greater than the number of elements in vector b from Example 1.10.
>> b(8)??? Attempted to access b(8); index out of bounds because numel(b)=5.
>> b(8)=8
b =
1 4 25 16 9 0 0 8
>> b(8)
ans = >> 8
In Example 7, the error message in the second line of the program code shows the use of the numel() function, which is used to check the number of elements in a vector (or array). Similarly, functions like size(), length(), ndims(), etc., can also be used to check information about vectors (or arrays).
The above content is from “Advanced Optical Simulation (MATLAB Version) – Optical Waveguides, Lasers” (3rd Edition)