0% found this document useful (0 votes)
3 views

Matlab_Complement

This document is a MATLAB lab exercise guide that covers operations and functions on matrices, including addition, multiplication, and various numerical operations. It provides examples of commands and their outputs, such as finding maximum values and sorting arrays. Additionally, it includes exercises for users to practice creating vectors, performing calculations, and demonstrating mathematical properties using MATLAB.

Uploaded by

datenshiyu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Matlab_Complement

This document is a MATLAB lab exercise guide that covers operations and functions on matrices, including addition, multiplication, and various numerical operations. It provides examples of commands and their outputs, such as finding maximum values and sorting arrays. Additionally, it includes exercises for users to practice creating vectors, performing calculations, and demonstrating mathematical properties using MATLAB.

Uploaded by

datenshiyu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercices MATLAB

Lab 2
M. Hosni
December 2024

MATLAB Operations on Matrices

Table 1: MATLAB operations on matrices.


Operation Symbol Example Output
Addition/Subtraction +/− A+B Sum/Difference of the two matrices
Addition of a +/− A−9 Subtracts 9 from each element of A
scalar
Multiplication by a ∗ 4∗A Multiplies every element of A by 4
scalar
Matrix ∗ A∗B Matrix multiplication
multiplication
Hadamard product ·∗ A · ∗B Element-wise multiplication of A and
B

The following numerical operations are carried out on every element of the matrix: power (.),
square root, logarithm, exponent, sine, cosine.

MATLAB Functions for Matrices


† For a matrix, the operation will be carried out separately on each column. For a vector (row or
column), the operation will be carried out on the vector.

MATLAB Example: Index of Maximum Value


In addition to the minimum or maximum values returned by the respective commands, MATLAB
returns the exact index where this value has been encountered. For example:
1 a = [3 1 9 5 2 1];
2 m = max ( a )

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.

4. Find the sum of the integers from 1 to 100.


5. Create an example to demonstrate that matrix multiplication is not commutative.
6. Create an example to demonstrate that the following equation holds:

(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π.

You might also like