Você está na página 1de 3

2

(1) Matrix operation


Table below gives the use of a colon in addressing arrays in a matrix.
Colon use for a matrix
Command
Description
V(:)
Refers to all the elements of the vector V.
V(m:n)
Refers to elements m through n of the vector V.
A(:, n)
Refers to the elements in all the rows of a column n of the matrix A.
A(n, :)
Refers to the elements in all the columns of row n of the matrix A.
A(:, m:n)
Refers to the elements in all the rows between columns m and n of the matrix A.
A(m:n, :)
Refers to the elements in all the columns between rows m and n of the matrix A.
A(m:n, p:q) Refers to the elements in rows m through n and columns p through q of the
matrix A.
Example: from the matrix a, write the commands that gives solution of question below in
command window.



=

[
[1 2

4 1

-i]

+

]
a

[6 2 3 -6 -i]

ii

[ 3 4 8 1-i 1+i]
3i

iii
3

iv
v

vi

Solution:
>> a=[3 1 8 2 6+i;1 2 9 5 7-i;6 4 7 3 5+2i;0 0 0 0 6-4i;1 6 2 4 i];
>> a(:,2)=[1 2 4 1 -i]
>> a(1,:)= [6 2 3 -6 -i]

>> a(6,:) = [ 3 4 8 1-i 1+i]


>> a(3,4) = 3i
>> abs(a(:,5))
>> a(3:4,3:4)
1 of 3 Computer application 2

8th December 2015

Eng. Mazen khaled

(2) Arithmetic operations:


The symbols for arithmetic operations with scalars are summarized below in Table below.

(3) Display formats


MATLAB has several different screen output formats for displaying numbers. These formats
can be found by typing the help command: help format in the Command Window. A few of these
formats are shown in Table below.
Command
Description
Example
Fixed-point with 4 decimal digits
>> 351/7
format short
ans = 50.1429
Fixed-point with 14 decimal digits
>> 351/7
format long
ans = 50.14285714285715
Scientific notation with 4 decimal digits >> 351/7
format short e
ans = 5.0143e + 001
Scientific notation with 15 decimal digits >> 351/7
format long e
ans = 5.014285714285715e001
Two
decimal
digits
>> 351/7
format bank
ans = 50.14
Best of 5 digit fixed or floating point
>> 351/7
format short g
ans = 50.143
Best of 15 digit fixed or floating point
>> 351/7
format long g
ans = 50.1428571428571
Put a number in ration form
>> 351/7
format rat
ans = 351/7
Example: Consider the two matrices:

Using MATLAB, determine the following:


(a) A + B (b) AB
(c) A2
(d) AT (e) B1
(f ) BTAT (g) A2 + B2 AB
(h) Determinant of A, determinant of B and determinant of AB.

2 of 3 Computer application 2

8th December 2015

Eng. Mazen khaled

Example: Determine the values of x1, x2, x3, x4 for the following set of linear algebraic
equations:

(4) Round-off functions:


Function
Description
Round to the nearest integer
round(x)
fix(x)

Round towards zero

ceil(x)

Round towards infinity

floor(x)

Round towards minus infinity

rem(x,y)

Returns the remainder after xis divided by y

sign(x,y)

Signum function. Returns 1 if x> 0, 1 if x< 0,


and 0 if x= 0.

3 of 3 Computer application 2

8th December 2015

Example
>> round(20/6)
ans = 3
>> fix(13/6)
ans = 2
>> ceil(13/5)
ans = 3
>> floor(10/4)
ans = 3
>> rem(14,3)
ans = 2
>> sign(7)
ans = 1

Eng. Mazen khaled

Você também pode gostar