Matlab Tutorial 2: Matrices in Matlab


Exercise 7: Matrix multiplication in Matlab

Matrix multiplication as you recognize them from different mathematical or applied courses are separated from element to element multiplication which we talked about earlier. The same goes for matrix division. Assume we have an equation system like:

1w + 3y + 5z = 1
1w + 0y + 1z = 0
5w + 0y + 9z = 1

Collect the coefficients to the left of the equality sign in a matrix A and the coefficients to the right in a column vector b. Then we will have an equation looking like:

AX = b, where X is a column vector containing the unknown w, y and z.

>> C=[ x' y1' y2']

This can mathematically be written like: A-1AX = A-1b where A-1A = I. I stands for the identity matrix.

Therefore we have: X = A-1b

The equation can easily be solved in Matlab by:

C(:,1) % : means all rows and 1 stands for the first column.
C(2,:) % means the second row and all columns.

What will the solution be for our equation system? Please check the solution.

The scalar product of two vectors (they must have the same length). Define the following:

>> plot(C(:,1), C(:,2),C(:,1),C(:,3)), grid
>> ones(3) % Gives a quadratic matrix, with three columns and three rows
% only containing ones.

If two vectors are orthogonal to each other their scalar product is zero. The product comes from element wise multiplication and then addition of the resulting elements.

>> ones(3,5) % Same as above, but with three rows and five columns.

or in this specific case as:

>> zeros(2) % Gives a quadratic matrix 2X2, and with zero as elements.
>> eye(3) % Gives the unity matrix with ones on the main diagonal.

Relation Matlab contains the possibility of comparing matrices. You can compare a matrix with a scalar. Matlab have 6 boolean operators for comparision. Relation operators:

  • < Less than
  • <= Less or equal with
  • > Greater than
  • >= Greater or equal than
  • == Equal with
  • ~= Separated from

If we use these operators there will be a new matrix as a result, containing zeros or ones. One means that the relation is true for that element, otherwise zero.

Exercise 8: Comparing matrices in Matlab

Introduce two new vectors A and B. See below !

>> x=reshape( C,9,1) % gives a column vector.

and

>> x=reshape(C,1,9) % gives a row vector.

Evaluate the following:

>> A==B
>> A~=B
>> A>=B

Logical operations We also have logical operators in matlab working with matrices and that results in new matrices.

  • A & B And operation: returns a matrix, with ones where both A and B have nonzero elements.
  • A | B Or operation: returns a matrix, with ones where A or B have nonzero elements.
  • ~B Negation , returns a matrix which gives ones where B contains zeros.
  • xor(A,B) Exclusive or, returns a matrix where an element with a one meaning that one of the matrices A and B has a nonzero element.

Practice the operators above.

Another very useful command is find. You can use it to locate elements in the matrix which are greater than or less than a certain value.

Example:

>> [row, column]=find (B) % gives row and column indices for those elements
% which are greater than 0.
>> [row, column]=find (1.1 > A > 0.9)% gives us row and column 
% indices for those elements less than 1.1 and greater than 0.9.
row= column= % A(1,1) and A(2,3) satisfies the conditions.
1 1
2 3
[row, column, value]=find(A==1) % gives us which elements are equal to 1.
row= column= value=
1 1 1
2 3 1

Pages: 1 2 3 4 5