A one-dimensional array can be viewed as a series of values arranged in a single row or column, with a single index used to select array elements. Such arrays can be used to describe data for functions with independent variables, for example, a series of temperature measurements taken at fixed time intervals.
A two-dimensional array is a matrix that can be accessed using two indices: the first index selects the row, and the second index selects the column.
How are two-dimensional arrays stored in computer memory?
A two-dimensional array with m rows and n columns will contain m x n elements, which will occupy m x n contiguous locations in computer memory. So, how are the elements of the array arranged in computer memory?MATLAB always allocates array elements in column-major order.That is, MATLAB always allocates the first column in memory first, then the second column, then the third column, and so on, until all columns are allocated. For example:
>> a = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
a =
1 2 3 4 5 6 7 8 9 10 11 12
The element a(1, 2) = 2 is the fifth to be allocated in memory.
Accessing Multidimensional Arrays in One Dimension
One of the features of MATLAB is that it allows users or programmers to view multidimensional arrays as one-dimensional arrays, with a length equal to the number of elements in the multidimensional array. If a multidimensional array is accessed using a single dimension, the elements will be accessed in the order they are allocated in memory.
For example, in the above array a, a(5) and a(1, 2) both have the value of 2, because a(1, 2) is the fifth to be allocated.
>> a(1, 2)
ans =
2
>> a(5)
ans =
2
In general, it is advisable to avoid using this feature of MATLAB. Accessing multidimensional arrays with a single index can lead to confusion.
Subarrays
A subset of a MATLAB array can be used as an independent array. To select a portion of an array, simply include a list of all the elements to be selected in parentheses following the array name. For example, suppose the array arr1 is defined as follows:
>> arr1 = [1.1 -2.2 3.3 -4.4 5.5];>> arr1(3) % Get the third element of array arr1
ans =
3.3000
>> arr1([1 4]) % Get the first and fourth elements of array arr1
ans =
1.1000 -4.4000
>> arr1(1 : 2 : 5) % Get the first, third, and fifth elements of array arr1
ans =
1.1000 3.3000 5.5000
For two-dimensional arrays, a colon can be used in the index to select all values at that index. For example:
>> arr2 = [1 2 3; -2 -3 -4; 3 4 5]
arr2 =
1 2 3 -2 -3 -4 3 4 5
>> arr2(1, :) % Get all elements of the first row of arr2
ans =
1 2 3
>> arr2(2, :) % Get all elements of the second row of arr2
ans =
-2 -3 -4
>> arr2(:, 2) % Get all elements of the second column of arr2
ans =
2 -3 4
>> arr2([1 3], :) % Get all elements of the first and third rows of arr2
ans =
1 2 3 3 4 5
>> arr2(:, [1 3]) % Get all elements of the first and third columns of arr2
ans =
1 3 -2 -4 3 5
>> arr2(:,:) % Get all elements of arr2
ans =
1 2 3 -2 -3 -4 3 4 5
The end Function
When using the end function in an array index, end returns the highest value taken by that index. For example, suppose the array arr3 is defined as follows:
>> arr3 = [1 2 3 4 5 6 7 8];>> arr3(5 : end) % Get all elements of arr3 starting from the fifth element
ans =
5 6 7 8
>> arr3(end) % Get the last element of arr3
ans =
8
The return value of the end function is always the maximum value of the given index. If end appears in different indices, it can return different values. For example, suppose the array arr4 is defined as follows:
>> arr4 = [1 2 3 4; 5 6 7 8; 9 10 11 12]
arr4 =
1 2 3 4 5 6 7 8 9 10 11 12
>> arr4(2 : end, 2 : end) % If end appears in different indices, it can return different values
ans =
6 7 8 10 11 12
Using Subarrays on the Left Side of Assignment Statements
Subarrays can be used on the left side of assignment statements to update certain values in the array, as long as the shape (number of rows and columns) of the assigned values matches the shape of the subarray. If the shapes do not match, an error will occur. For example, suppose the array arr4 is defined as follows:
arr4 =
1 2 3 4 5 6 7 8 9 10 11 12
>> arr4(1 : 2, [1 4]) = [20 21; 22 23]
arr4 =
20 2 3 21 22 6 7 23 9 10 11 12
>> arr4(1 : 2, 1 : 2) = [3 4]Unable to perform assignment because the size of the left side is 2-by-2 and the size of the right side is 1-by-2.
In MATLAB, there is a clear distinction between assigning to subarrays and assigning to arrays. If assigning to a subarray, only some values are updated, while other values in the array remain unchanged. If assigning to an array, the entire content of the array will be deleted and replaced with new values. For example:
>> arr5 = [1 2 3 4; 5 6 7 8; 9 10 11 12]
arr5 =
1 2 3 4 5 6 7 8 9 10 11 12
>> arr5(1 : 2, [1 4]) = [20 21; 22 23] % Assigning to a subarray, some values change
arr5 =
20 2 3 21 22 6 7 23 9 10 11 12
>> arr5 = [1 2; 3 4] % Assigning to an array, the new array replaces the old array
arr5 =
1 2 3 4
Assigning Scalar Values to Subarrays
The scalar value on the right side of the assignment statement always matches the shape specified on the left side. The scalar value is assigned to each element specified on the left side of the statement. For example:
>> arr6 = [1 2 3 4; 5 6 7 8; 9 10 11 12]
arr6 =
1 2 3 4 5 6 7 8 9 10 11 12
>> arr6(1 : 2, [1 4]) = 1
arr6 =
1 2 3 1 1 6 7 1 9 10 11 12
Special Values
MATLAB includes many predefined special values. These predefined values can be used at any time in MATLAB without initialization.
| Function | Effect |
|---|---|
| pi | Represents the value of pi, accurate to 15 decimal places |
| i, j | Represents the square root of -1 |
| Inf | Represents infinity, typically the result of division by zero |
| NaN | Represents ‘Not a Number’, the result of an undefined mathematical operation, such as 0 divided by 0 |
| clock | Represents a row vector of the current date and time, containing six elements: year, month, day, hour, minute, and second |
| date | Represents the current date in string format |
| eps | Is an abbreviation for ‘epsilon’, which is the smallest difference between two numbers in the computer |
| ans | Represents a special variable that stores the result of an expression that has not been explicitly assigned to another variable |
Do not redefine the meanings of predefined variables in MATLAB. This can lead to hard-to-detect errors.
Happy learning to everyone~~