0% found this document useful (0 votes)
67 views38 pages

The Transpose Operator

This document discusses various matrix operations in MATLAB like transposition, element-wise operations, indexing, extraction of elements and submatrices, and addition of elements. It provides examples of using special operators like colon, transpose, and dot operators for dealing with arrays and matrices. Common functions for minimum, maximum, trigonometric operations are also covered.

Uploaded by

narendra29000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views38 pages

The Transpose Operator

This document discusses various matrix operations in MATLAB like transposition, element-wise operations, indexing, extraction of elements and submatrices, and addition of elements. It provides examples of using special operators like colon, transpose, and dot operators for dealing with arrays and matrices. Common functions for minimum, maximum, trigonometric operations are also covered.

Uploaded by

narendra29000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

(The Transpose Operator)

>> C=[2 55 14 8; 21 5 32 11; 41 64 9 1]

C = 2 55 14 8

21 5 32 11

41 64 9 1

>> D=C'

D = 2 21 41

55 5 64

14 32 9

8 11 1
1
(Vectors Arithmetic 2)
 Addition or subtraction of matrices is possible as long as they
are the same size.
>> [1 2 3]+[4 5 6]
ans = 5 7 9

 To deal with arrays on an element-by-element level we need to use


the following array or dot-operators:
.* , ./ and .^

 Element-by-element multiplication and division uses the .*


and ./ operators. The * and / operators are used for complete
matrix multiplication and division.
>> [1 2 3].*[4 5 6]
ans = 4 10 18
2
dot-operators
.* , ./ and .^
Because scalars are equivalent to a 1×1 array, you can
either use the standard or the dot-operators when doing
multiplication, division and exponentiation of scalars (i.e.,
of single numbers).
It is okay for you to always use the dot-operators, unless
you intend to perform vector or matrix multiplication or
division.
Array vs. Matrix Operations
 Example: m=[2,1;3,4] p=[5,6;7,8] (in class we had a look on
m=[1,2;3,4] and results were different )

q=m.*p

results in [10, 6; 21, 32]; this is array multiplication


q = m * p
results in [17, 20; 43, 50]; this is matrix multiplication

So, do NOT forget the dot when doing array operations!


(.* ./ .^)
(More about Column Vectors)
 The vectors seen so far are row vectors (horizontal).
You can define column vectors as well. For row vectors
you separate the elements with spaces or commas; for
column vectors you use semi-colons.
>> colvec = [1;2;3]
colvec = 1
2
3

5
(More about Matrices)
 You can define matrices the same way by combining rows
and columns.
>> mat1 = [1 2;3 4]
mat 1 = 1 2
3 4
>> mat2 = [mat1;mat1]
mat2 = 1 2
3 4
1 2
3 4

6
(The .^ and sqrt Commands)
 .^ us used for the element-by-element power operator
(works like .* and ./ seen in slide 25).
>> [1 2 3].^2
ans = 1 4 9
 Square roots are done with sqrt (works with negative
numbers too!).
>> sqrt (-9)
ans = 0 + 3.0000i
>> sqrt ([1 2 4 9])
ans = 1.0000 1.4142 2.0000 3.0000

7
(Trig Commands)
 Trig functions are supported like sin.
>> sin ([pi/4 pi/2 pi])
ans = 0.7071 1.0000 0.0000
 A few more command examples... There are many many
more!
 log(x), log10(x), cos(x), atan(x), exp(x), round(x),
floor(x), ceil(x), angle(x), abs(x)...
 They work exactly like the math library functions in C.
Need help with the sin function? Try help sin or doc sin.

8
(Saving and Loading Variables)
 Use save to save variables to a file.
>> save myfile mat1 mat2
saves matrices mat1 and mat2 to the file myfile.mat

 To load saved variables back into the workspace, use


load.
>> load myfile

 If the command window is too full, use clc.

9
(More about Transpose)
 The transpose operators turn a column vector into a row
vector and vice versa as seen before. For example to
transpose a vector x,

>> transpose (x);


or
>> x'

 You can also use x.' if you want to transpose a vector


containing complex numbers.

10
(Example: Scalar Product)
 A scalar product can be done with one row vector
by a column vector. A transpose may be required.

>> scalarprod = [1 2 3 4] * [4 5 6 7]'


scalarprod = 60

11
(Multiple Initializations)
 It is possible to perform multiple initializations at once:

>> v1 = ones (1,10)


v1 = 1 1 1 1 1 1 1 1 1 1

>> v1 = rand (1,100);


A row vector with 100 random elements between 0 and 1.

>> v1 = zeros (45,1);


A column vector with 45 zero elements.

12
(Index vs. Subscript)
 For a vector, indexes and subscripts are the same.
Remember that indexes and subscripts start at 1 in
MATLAB.
 For matrices, subscripts have the row and column
numbers separated by a comma. Indexes, on the other
hand, indicate the linear position from the start of the
matrix.
subscripts indexes

13
(Matrix Indexing)
 The index argument can be a matrix. In this case,
each element is looked up individually, and
returned as a matrix of the same size as the index
matrix.

>> a = [10 20 30 40];


>> b = a([1 2 4;3 4 2])
b = 10 20 40
30 40 20

14
(Working Whole Rows or Columns)
 The colon operator selects rows or columns of a matrix.
>> c = b (1,:)
c = 10 20 40

>> d = b (:,2)
d = 20 40

>> b (:, 3) = [100 200]


b = 10 20 100
30 40 200

15
(Colon Operator)
The colon : lets you address a range of elements

• Vector (row or column)

 va(:) - all elements

 va(m:n) - elements m through n

• Matrix

 A(:,n) - all rows of column n

 A(m,:) - all columns of row m

 A(:,m:n) - all rows of columns m through n

 A(m:n,:) - all columns of rows m through n

 A(m:n,p:q) - columns p through q of rows m through n


16
17
(Colon Operator)
The colon : lets you address a range of elements

• Vector (row or column)

 va(:) - all elements

 va(m:n) - elements m through n

• Matrix

 A(:,n) - all rows of column n

 A(m,:) - all columns of row m

 A(:,m:n) - all rows of columns m through n

 A(m:n,:) - all columns of rows m through n

 A(m:n,p:q) - columns p through q of rows m through n


18
Colon Operator
 Colon notation can be used to define evenly spaced vectors in the form:
first : last mm=1:3 mm=
1 2 3

 The default spacing is 1. To use a different increment use the form:

first : increment : last y=1:2:7 y=


1 3 5 7

 The numbers now increment by 2

19
Extracting Data
 The colon represents an entire row or column when used in as an array
index in place of a particular number.

n = n(:,1) n(2,:)
2 50 20 ans =
1 1 2 ans = 1 1 2
2
0 55 66
1
The colon operator can also be used to extract 0
a range of rows or columns:

n(2:3,:) n(1,2:3)
ans = ans =
1 1 2 50 20
0 55 66

20
(Finding Minimum and Maximum Values)
>> vec = [10 7 8 13 11 29];
 To get the minimum value and its index:

>> [minVal,minInd] = min(vec)


minVal = 7 minInd = 2
 To get the maximum value and its index:

>> [maxVal,maxInd] = max(vec)


maxVal = 29 maxInd = 6
>> [a,b] = max(vec)
a = 29 b = 6

21
(Getting Specific Columns)


You can replace vector index or matrix
indices by vectors in order to pick out
specific elements. For example, for
vector v and matrix m
• v([a b c:d]) returns elements a, b, and
c through d
• m([a b],[c:d e]) returns columns c
through d and column e of rows a and b

22
(Colon Operator Examples 1)

>> v=4:3:34
v = 4 7 10 13 16 19 22 25 28 31 34

>> u=v([3, 5, 7:10])


u = 10 16 22 25 28 31

>> u=v([3 5 7:10])


u = 10 16 22 25 28 31
23
(Colon Operator Examples 2)

>> A=[10:-1:4; ones(1,7); 2:2:14; zeros(1,7)]

A = 10 9 8 7 6 5 4

1 1 1 1 1 1 1

2 4 6 8 10 12 14

0 0 0 0 0 0 0

>> B=A([1 3],[1 3 5:7])

B = 10 8 6 5 4

2 6 10 12 14

24
Example?
A=
10 9 8 7 6 5 4
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0

B=A([1 3],:)
B= B=A([1 3])
10 9 8 7 6 5 4 B=
2 4 6 8 10 12 14 10 2

B=A(:,[1 3 5:7])
B=A([1 3],[1 3 5:7])
B=
B=
10 8 6 5 4
10 8 6 5 4
1 1 1 1 1
2 6 10 12 14
2 6 10 12 14
0 0 0 0 0

25
(Two ways to add elements to existing variables)
Assign values to indices that don't exist:
MATLAB expands array to include indices, puts the specified
values in the assigned elements, fills any unassigned new
elements with zeros.
Add values to ends of variables:
Adding to ends of variables is called appending or
concatenating.
"end" of vector is right side of row vector or bottom of
column vector.
"end" of matrix is right column and bottom row.
26
(Assigning to undefined indices of vectors)
>> fred=1:4

fred = 1 2 3 4

>> fred(5:10)=10:5:35

fred = 1 2 3 4 10 15 20 25 30 35

>> wilma=[5 7 2]

wilma = 5 7 2 New elements added

>> wilma(8)=4

wilma = 5 7 2 0 0 0 0 4

>> barney(5)=24

barney = 0 0 0 0 24 Unassigned elements


27 are initialized at zero
(Appending to vectors)
• You can only append row vectors to row vectors and
column vectors to column vectors.
If r1 and r2 are any row vectors,
r3 = [r1 r2] is a row vector whose left part
is r1 and right part is r2
If c1 and c2 are any column vectors,
c3 = [c1; c2] is a column vector whose top
part is c1 and bottom part is c2

28
>> pebbles = [3 8 1 24];

>> dino = 4:3:16;

>> betty = [pebbles dino]

betty = 3 8 1 24 4 7 10 13 16

>> bedrock = [pebbles'; dino']

bedrock = 3
8
1
24
4
7
10
13
29
16
(Appending to matrices)
• If appending one matrix to the right side of
another matrix, both must have same
number of rows.
• If appending one matrix to the bottom of
another matrix, both must have same
number of columns

30
>> A2=[1 2 3; 4 5 6]
A2 = 1 2 3
4 5 6
>> B2=[7 8; 9 10]
B2 = 7 8
9 10
>> C2=eye(3)
C2 = 1 0 0
0 1 0
0 0 1

31
>> Z=[A2 B2]

Z = 1 2 3 7 8

4 5 6 9 10

>> Z=[A2; C2]

Z = 1 2 3

4 5 6

1 0 0

0 1 0

0 0 1

>> Z=[A2; B2]

??? Error using ==> vertcat

CAT arguments dimensions are not consistent.

32
To delete elements in a vector or matrix, set range
to be deleted to empty brackets.
>> kt=[2 8 40 65 3 55 23 15 75 80]
kt = 2 8 40 65 3 55 23 15 75 80
>> kt(6)=[] Delete sixth element (55)

kt = 2 8 40 65 3 23 15 75 80 55 gone

>> kt(3:6)=[] Delete elements 3 through


6 of current kt, not
original kt
kt = 2 8 15 75 80

33
To delete elements in a vector or matrix, set
range to be deleted to empty brackets.
>> mtr=[5 78 4 24 9; 4 0 36 60 12; 56 13 5 89
3]

mtr = 5 78 4 24 9

4 0 36 60 12

56 13 5 89 3

>> mtr(:,2:4)=[] Delete all rows for


columns 2 to 4

mtr = 5 9

4 12

34 56 3
MATLAB has many built-in functions for working
with arrays. Some common ones are:
• length(v) - number of elements in a vector.
• size(A) - number of rows and columns in a
matrix or vector.
• reshape(A,m,n) - changes number of rows
and columns of a matrix or vector while keeping
total number of elements the same. For example,
changes 4x4 matrix to 2x8 matrix.

35
reshape Example
>> a = [1 2 ; 3 4]
a=
12
34

>> a = [a [5 6; 7 8]]
a= ans =
1256 1
3478
3
>> reshape (a, 1, 8) 2
ans = 4
13245768 5
7
>> reshape (a, 8, 1)
6
8
36
• diag(v) - makes a square matrix of zeroes with
vector in main diagonal.
• diag(A) - creates vector equal to main diagonal
of matrix.

For more functions, in help window select


"Functions by Category", then
"Mathematics", then "Arrays and Matrices".

37
Example$

>> a=[1 2 3]
>> B= [1 2 3;2 5 7; 0 0 8]
a=
B=
1 2 3
1 2 3
2 5 7
>> diag(a)
0 0 8
ans =
>> diag(B)
1 0 0
ans =
0 2 0
0 0 3
1
5
8
38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy