A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Saturday, February 6, 2016

Arrays and Matrices in MATLAB


MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. —Vectors are special forms of matrices and contain only one row or one column. —Scalars are matrices with only one row and one column.

Creating Matrices

In MATLAB, a vector is created by assigning the elements of the vector to a variable. This can be done in several ways depending on the source of the information.
  • —Enter an explicit list of elements 
  • —Load matrices from external data files 
  • —Using built-in functions 
  • —Using own functions in M-files
—A matrix can be created in MATLAB by typing the elements (numbers) inside square brackets [ ]
>>matrix = [1 2 3 ; 4 5 6 ; 7 8 9]

First open the [ bracket. Then type the elements in a row with a space or a comma between the elements and then enter the semicolon (or press Enter) to move to the next row. Finally close the ] bracket.

Example:

—>> A = [2 -3 5; -1 4 5]
—A =
— 2 -3 5
— -1 4 5
—>> x = [1 4 7]
—x =
— 1 4 7
—>> x = [1; 4; 7]
—x =
— 1
— 4
— 7

The Colon Operator and linspace Command

The colon operator can be used to create a vector with constant spacing (the difference between the elements is the same). The syntax is 
>>x= first:step:last
A vector with n elements that are linearly (equally) spaced in which the first element is xi and the last element is xf can be created by typing the linspace command. The syntax is 
>>x = linspace(xi, xf, n)

Example:

>> x=[1:2:10]
x =
     1     3     5     7     9   (If the last numbers cannot be obtained by adding steps to first, then the last element in the vector will be the number that does not exceed last)

>> 2:5
ans =
    2   3   4   5 (If only the first and the last terms are typed then the default for the step is 1.)

>> -2:3
ans =
   -2   -1   0   1   2   3

>> 0.2:0.5:2.4
ans =
    0.2000 0.7000 1.2000 1.7000 2.2000

>> -3:3:10
ans =
    -3   0   3   6   9

>> 1.5:-0.5:-0.5     (negative step is also possible)
ans = 
     1.5000 1.0000 0.5000 0 -0.5000

>>f= linspace(0, 10, 5)
f =
         0    2.5000    5.0000    7.5000   10.0000     (5 elements, from 0 to 10)

>>f= linspace(0, 10)          (When the number of elements is omitted, the default is 100. 100 elements will be dispalyed)
f =
  Columns 1 through 10
         0    0.1010    0.2020    0.3030    0.4040 ............................................
......................Columns 91 through 100
    9.0909    9.1919    9.2929    9.3939    9.4949    9.5960    9.6970    9.7980    9.8990   10.0000

Builtin Functions to Generate Matrices

Some special matrices can be created by using builtin functions. zeros (r, c), ones (r, c), eye (n) and rand(r, c) are the commands used to create matrix of r rows and c columns.

  • zeros (r, c)  - create a matrix with all zeros
  • ones (r, c)   - create a matrix with all ones
  • eye (n)        - create a identity matrix
  • rand(r, c)    - create a matrix with random numbers

Examples:

>> A = ones(3,2)
A =
     1     1
     1     1
     1     1
>> B = zeros(3,4)
B =
     0     0     0     0
     0     0     0     0
     0     0     0     0
>> C = rand(2,5)
C =
    0.8147    0.1270    0.6324    0.2785    0.9575
    0.9058    0.9134    0.0975    0.5469    0.9649
>> D = eye(3)
D =
     1     0     0
     0     1     0
     0     0     1
>> E = magic(4) - Durer’s matrix
E =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Functions to Handle Matrices and Arrays

x = [1 2 3];
y= [1 2 3; 4 5 6];

length( ) - number of elements in a array. number of columns in a matrix
>>length(y)
ans =
     3
>>length(x)
ans =
     3

sum( ) –  Sum of elements in a array. Sum of elements in a column of a matrix.
>>sum(x)
ans =
     6
>>sum(y)
ans =
     5     7     9

  – Transpose of a matrix. Convert a row vector to column vector
>>y'
ans =
     1     4
     2     5
     3     6
>> x'
ans =
     1
     2
     3

diag( ) – diagonal elements of matrix. crate a matrix with elements of a vector,if the argument is a vector)
>>diag(y)
ans =
     1
     5
>>diag(x)
ans =
     1     0     0
     0     2     0
     0     0     3

—size( ) – size (dimensions) of matrix
>>size(y)
ans =
2 3 (2 rows, 3 columns) 

det( ) – determinant of a matrix. (matrix must be square to get determinant)
>> z= [1 2 3; 4 5 6; 7 8 0];
>> det (z)
ans =
    27

inv( ) – inverse of a square matrix
>> inv(z)
ans =
   -1.7778    0.8889   -0.1111
    1.5556   -0.7778    0.2222
   -0.1111    0.2222   -0.1111
submit to reddit