Matrices

 MATLAB Matrices
 
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.

 Create a row and column vector
First, let's create a simple row vector with 9 elements called a.
a = [1 2 3 4 6 4 3 4 5]
a =

     1     2     3     4     6     4     3     4     5

Similarly to create a simple column vector with 5 elements  using semicolons (;) to separate the rows of a matrix.
b=[1;2;3;4;5]
b=
1
2
3
4
5
we have another functions to create row matrix..
1.  y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b.
Eg: linspace(5,7) generates a row vector y of 100 points linearly spaced between and including 5 and 7.
2.       linspace(a,b,c)
Eg: y=linspace(1,36,12) generates a row vector of 12 points which is start from 1 and terminate at 36
3.       logspace(a,b) generate a row vector y of 50 logarithmically spaced points between decades 10^a and 10^b.
4.      logspace(a,b,n) generates n points between decades 10^a and 10^b.
5.       colon operator(:) generates row vector with starting point a and end with b with 1 increment.
Eg:   D = 1:4
results in
D =
1       2    3    4

6.       a:b:c generates a row vector with starting point a and ending with c with  increment of b
E = 0:.1:.5
results in
E =
    0    0.1000    0.2000    0.3000    0.4000    0.5000

Create a Matrix
Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.
A = [1 2 0; 2 5 -1; 4 10 -1]
A =
 
     1     2     0
     2     5    -1
     4    10    -1
Transpose of Matrix
We can easily find the transpose of the matrix A.
B = A'
B =
 
     1     2     4
     2     5    10
     0    -1    -1
 
Inverse of Matrix
 
we can easily find the inverse of the matrix
>> a=[0 1 2;1 2 3 ; 3 1 1 ]

a =

     0     1     2
     1     2     3
     3     1     1

>> inv(a)

ans =

    0.5000   -0.5000    0.5000
   -4.0000    3.0000   -1.0000
    2.5000   -1.5000    0.5000
.
 we have following types to create a matrix.
·         a=[1 2 3 ; 3 4 5; 5 6 7]
a =

     1     2     3
     3     4     5
     5     6     7
·         M=magic(n)
M = magic(n) returns an n-by-n matrixconstructed from the integers 1 through n^2 with equal row and column sums
M = magic(3)
 
M = 
 
    8    1    6
    3    5    7
    4    9    2

This is called a magic square because the sum of the elements in each column is the same.
 sum(M) =
 
     15    15    15

Create random Matrix
1.       M=rand(r,c)    or   rand(n)
r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1).
NOTE: if we want to create a random matrix in which we have same elements the use ‘rng’.
Eg:
>> s=rng

s =

     Type: 'twister'
     Seed: 0
    State: [625x1 uint32]

>> a=rand(3)

a =

    0.3922    0.7060    0.0462
    0.6555    0.0318    0.0971
    0.1712    0.2769    0.8235

>> rng(s)
>> f=rand(3)

f =

    0.3922    0.7060    0.0462
    0.6555    0.0318    0.0971
    0.1712    0.2769    0.8235


rand(3)

ans =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575
rand (m,n) returns m by n matrix in interval 0 to 1

>> rand(2,3)

ans =

    0.9649    0.9706    0.4854
    0.1576    0.9572    0.8003
2. r = randn(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard normal distribution from – inf. To +inf.
3. a=randi(max,r,c) generate a square matrix of ‘a’ which contains values from 1 to max
4. r=randi(100,1,5) generates the row vector which contains values randomly from 1 to 100 which have one row and 5 columns.
Eg:  randi(100,5,10)
ans =

    55    52    19    95    37    41    58     2    65    19
    15    41    24    50    12    10     6     5    46    69
    86     8    42    49    79    14    24    17    55    19
    63    24     5    34    39    95    36    65    30    37
    36    13    91    91    25    96    83    74    75    63

>> randi(100,1,5)

ans =

    79     9    93    78    49
NOTE: if we want a matrix between desire value(for eg. 5 to 10) then…
Randi([5 10],3,4)


randi([5 10],3,4)

ans =

     7     8     5     7
     8     9     6     8
     6     6     6    

One Matrix

·         a=ones(r,c)/ones(n)
create a one matrix which contain n by n one
ones(2,3)

ans =

     1     1     1
     1     1     1

>> ones(4)

ans =

     1     1     1     1
     1     1     1     1
     1     1     1     1
     1     1     1     1

Zero Matrix

·         a=zeros(r,c)/zeros(n)
zeros(3)

ans =

     0     0     0
     0     0     0
     0     0     0

>> zeros(3,4)

ans =

     0     0     0     0
     0     0     0     0
     0     0     0     0

Arithmatic operation with Matrix
1.       Multiplication
C = A * B
C =

     5    12    24
    12    30    59
    24    59   117
Note: Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or vectors using the .* operator.
C = A .* B
C =
 
     1     4     0
     4    25   -10
     0   -10     1
 
How to access the particular element and row or column of the matrix…
a=rand(5)

a =                                  

   (1) 0.2373   (6) 0.2316   (11) 0.3674   (16) 0.7962   (21) 0.1366
    (2)0.4588    (7)0.4889    (12)0.9880    (17)0.0987    (22)0.7212
    (3)0.9631    (8)0.6241    (13)0.0377    (18)0.2619    (23)0.1068
    (4)0.5468    (9)0.6791    (14)0.8852    (19)0.3354    (24)0.6538
    (5)0.5211    (10)0.3955    (15)0.9133    (20)0.6797    (25)0.4942


>> a(2,3)

ans =

    0.9880

>> a(12)

ans =

    0.9880
How to access the row or column of the matrix : by using colon operator(:)
Eg: a(r,:) use for fetch the row of the matrix
Eg : a(:,c) use for fetch the column of the matrix
>> a=magic(5)

a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> a(:,3)

ans =

     1
     7
    13
    19
    25

>> a(2,:)

ans =

    23     5     7    14    16

How to fetch the desire elements from a row or column of a matrix.

>> a=magic(5)

a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> a(1:5,4)

ans =

     8
    14
    20
    21
     2

>> a(1,1:5)

ans =

    17    24     1     8    15

How to find the maximum values in a matrix as a vector .
a =

    0.7791    0.7150    0.9037    0.8909    0.3342
   23.0000    5.0000    7.0000   14.0000   16.0000
    4.0000    6.0000   13.0000   20.0000   22.0000
   10.0000   12.0000   19.0000   21.0000    3.0000
   11.0000   18.0000   25.0000    2.0000    9.0000

>> max(a)

maximum=

    23    18    25    21    22
Note : to find the largest number of the matrix  :  take again of the max of the row vector
>> max(maximum)

largest =

    25
similarly in case of minimum.

Reshape of the matrix.
we can easily reshape the matrix  but one thing we have to remember that
 B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements
Reshape a 3-by-4 matrix into a 2-by-6 matrix.
A =
    1    4    7    10
    2    5    8    11
    3    6    9    12
         
B = reshape(A,2,6)
         
B =
    1    3    5    7    9   11
    2    4    6    8   10   12
How to delete the row or column of the matrix..
use [ ] operator and put equals the row or column which we want to delete but not a particular element bcoz matrix will become misbalance…
a =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> a(1, :)=[ ]

a =

    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
Find the dimension of the matrix.
use size(a)

>> o=rand(4,5)

o =

    0.5005    0.0424    0.8181    0.6596    0.8003
    0.4711    0.0714    0.8175    0.5186    0.4538
    0.0596    0.5216    0.7224    0.9730    0.4324
    0.6820    0.0967    0.1499    0.6490    0.8253

>> size(o)

ans =

     4     5
Fetch the diagonal element of the matrix.

>> f=magic(6)

f =

    35     1     6    26    19    24
     3    32     7    21    23    25
    31     9     2    22    27    20
     8    28    33    17    10    15
    30     5    34    12    14    16
     4    36    29    13    18    11

>> diag(f)

ans =

    35
    32
     2
    17
    14
    11
 


No comments:

Post a Comment