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:

>> x(25:28)=[] % Indices 25 to 28 are eliminated.
>> x(25.28)=0 % Indices 25 to 28 are assigned to zero.
>> randn(1,500) % Creates a random vector with normal distribution,
% mean zero, variance one and standard deviation one.
% X is a row vector, that is 1 row and 500 columns.

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:

>> A=[ 1 2; 3 4]; B=[ 1 2; 3 4];

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.

>> A+ B
 
ans=
2 4
6 8

and

>> A-B
ans=
0 0
0 0

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:

>> A*B % matrix multiplication
ans =
7 10
15 22
>> A.*B % multiplication of corresponding elements in matrix A and B.
ans =
1 4
9 16
>> A/B % matrix division gives identity matrix.
ans =
1 0
0 1
>> 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.

Related posts:

  1. Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types
  2. Matlab Tutorial 3: Strings in Matlab
  3. Matlab Tutorial 4: Data Analysis and Statistics with Matlab
  4. Matlab Tutorial 7: Common Programming Structures and Conditional Statements

Pages: 1 2 3 4 5

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment