Matlab_Complement
Matlab_Complement
Lab 2
M. Hosni
December 2024
The following numerical operations are carried out on every element of the matrix: power (.),
square root, logarithm, exponent, sine, cosine.
Returns 9 in m. If, however, the command is invoked with two output arguments:
1
Table 2: MATLAB functions for matrices.
Command Return
a’ Transpose of a
find(a) Indices of all non-zero elements in a
fliplr(a) Matrix a, flipped horizontally
flipud(a) Matrix a, flipped vertically
inv(a) Inverse of a
min(a) Minimum-valued element of a. †
max(a) Maximum-valued element of a. †
numel(a) The number of elements of a
repmat(a, m, n) A matrix where a is repeated in m rows and n columns
reshape(a, m, n) Matrix a reshaped into m rows and n columns
size(a) The size of a (#rows, #columns, . . . )
sort(a) Vector a sorted into ascending order
sum(a) Sum of elements of a. †
unique(a) The list of unique elements of a in ascending order
1 [m , i ] = max ( a )
MATLAB will return 9 in m and 3 in i because a(3) holds the largest value 9. Recent versions
of MATLAB (after 7.9, 2009b) allow for replacing unnecessary arguments with a tilde (~). If only
the place of the maximum is of interest, you can use:
1 [~ , i ] = max ( a )
If a minimum or maximum value appears more than once in the array, only the index of the
first occurrence will be returned. In the example above, the minimum value 1 sits in positions 2
and 6. Only 2 will be returned as the second output argument of the min command.
Similarly, the sort command returns as the second argument a permutation of the indices
corresponding to the sorted vector. For example:
1 >> [p , i ] = sort ( a )
2
3 a = 3 1 9 5 2 1
4 p = 1 1 2 3 5 9
5 i = 2 6 5 1 4 3
In this example, p contains the sorted a. p(1) is 2 because the first occurrence of the minimum
element 1 is at position 2. The next smallest element is the 1 at position 6, hence the second entry
in p. The third smallest element, 2, is in position 5, and so on.
Exercises
1. Create a 4 × 1 column vector that contains any values of your choosing.
2
2. Create a cell array of 8 elements such that element i contains the identity matrix of size i,
i = 1, . . . , 8.
3. Use one MATLAB command to evaluate the sine of 30◦ , 45◦ , 60◦ , and 120◦ . Subsequently,
evaluate cosine, tangent, and cotangent of the same angles.
(ABC)T = C T B T AT ,
where A, B, and C are matrices of different sizes, and the product ABC is feasible.
7. Evaluate the following expression:
T
10 −7 6 −9 4 −2 5 −9 5 4 −7 −3
6 0 −1 10 7 − 8 6 4 −9 −8 × 6 4 0 2
7 9 4 9 5 −6 −4 7 −4 −6 10 −5
8. Create a 1 × 6 vector v containing the integer values from 20 to 25. Subsequently, create a
1 × 6 vector whose values are equal to 5 times the values in v.
9. Create a vector that goes at equal steps from −2 to +2 containing 50 components.
10. Create a vector spanning the range from 0 to 2π, containing 100 equally spaced components,
so that the first value is 0, and the last value is 2π.