Matlab Tutorial 2: Matrices in Matlab


If we would like to decrease the number of elements in the vector we can simply use the array editor and choose the indices that we want to eliminate or by assigning certain indices to become empty. Like:

>> C=[ x' y1' y2']
C(:,1) % : means all rows and 1 stands for the first column.
C(2,:) % means the second row and all columns.
>> plot(C(:,1), C(:,2),C(:,1),C(:,3)), grid

Exercise 2: Arithmetical operations on vectors

Arithmetic operations can be performed on matrices, but in some cases they are carried out as element by element operations. Let us show you some examples:

>> ones(3) % Gives a quadratic matrix, with three columns and three rows
% only containing ones.

The traditional operations like addition and subtraction assumes the same size of the matrices. As you can see below addition and subtraction is done between the corresponding elements in matrix A and B.

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

and

>> zeros(2) % Gives a quadratic matrix 2X2, and with zero as elements.

For the operations /, * and ^ we must be cautious. Because they have a meaning as well as for the matrix as for the elements. Note the following:

>> eye(3) % Gives the unity matrix with ones on the main diagonal.
>> x=reshape( C,9,1) % gives a column vector.
>> x=reshape(C,1,9) % gives a row vector.
>> A./B % division between the corresponding elements in matrix A and B.
ans =
1 1
1 1

and finally:

>> A.^B % each element in A 
%raised to power of the corresponding element in B.
ans =
1 4
27 256

It is of course possible to multiply a matrix by a scalar. This means what ?

>> 2*A

Get aquatinted with the commands in the table below to calculate the following for the matrix

A=[ 1 2 3; 4 5 6; 7 8 9];
  1. sum of all elements in the matrix A. % answer=45
  2. product of all elements in the matrix A. % answer=362 880
  3. the largest element in the matrix A % answer= 9
  4. the smallest element in the matrix A % answer= 1

Elementary commands operating on matrices.

  • max(A): Gives the largest value from each column.
  • min(A): Gives the smallest value from each column.
  • sum(A): Adds all the elements in each column.
  • prod(A): Gives the product of all elements in a column.
  • mean(A): Calculates the mean value of each column.
  • sort(A): Sorts the elements in each column after size.
  • transpose(A) or A’: switches the columns versus rows. Makes the transpose of A.

 

Exercise 3: More operations with matrices

Write a simple m-file that produces a table containing one column with x-values and two others with square root values and square respectively. Below is a skeleton of m-file, but you should try to finish it.

1
2
3
4
5
6
7
8
9
% exercise 3.m created by MatlabCorner.com
% The m-file creates a table with: x, sqrt(x), x^2.
clear % clear all variables from workspace.
x=1:5 % creates a vector with elements 1,2,3,4 and 5
y1=...............
y2=..............
disp('x sqrt(x) x^2') % writes the text within the parenthesis.
disp([x' y1' y2']) % makes a table of three columns
% from three transposed vectors.

Plot columns two and three versus the x-vector.

>> plot(x,y1,x,y2), grid % plots two curves.

Pages: 1 2 3 4 5