Sunteți pe pagina 1din 32

Chap.

M.

Arrays transpose operationArrays transpose operation

Transpose operation

a vector x
x = 1

2 5

= [1 2 5 1]

transpose y

= x

y = 11

Transpose operation
z = [1; 2; 3; 4]
z=
1 2 33 4
z'
ans =
1 2

Basic Vector Operations


Operations such as vector addition,
subtraction and scalar multiplication can
easily be done in Matlab

Basic Vector Operations


Also, element-by-element multiplication

>> x = 1:10; >> term = 1./sqrt(x);>> term = 1./sqrt(x);


>> y = sum(term)

Logical Operators
You can use Logical Indexing to find data
that conforms to some limitations
Logical Operators:
Greater Than: >
Less Than: <
Greater Than or Equal To: >=
Less Than or Equal To: <=
Is Equal: ==
Not Equal To: ~=

Built-in Functions

Functions: MATLAB provides a large number of standard


elementary mathematical functions, including abs (absolute),
sqrt (square root), exp exponential), Inf (infinity), pi ( =
3.141592L), NaN (not a number), i or j ( 1 ), cos (cosine)
and sin (sine). Infinity is generated by dividing a nonzero value
by zero. Not-a-Number is generated by trying to evaluate
expressions like 0/0 or that do not have well defined
mathematical values. Also taking the square root or logarithm of
a negative number is not an error; the appropriate complex
result is negative number is not an error; the appropriate
complex result is produced automatically.
1

Use built-in functions: MATLAB provides four


functions that generate basic matrices. zeros(m,n):
Create a matrix (m n ) of all zeros ones(m,n): Create
a matrix (m n ) of all one rand(m,n): Returns a
matrix (m n ) of values derived from a rand(m,n):
Returns a matrix (m n ) of values derived from a
uniform distribution on the unit interval ( 0 all value
1). randn(m,n): Returns a matrix (m n ) of values
derived from a normal distribution with mean 0 and
standard deviation 1.
1

Generating Vectors from


functions

zeros(M,N) MxN matrix of zeros

ones(M,N) MxN matrix of ones

numbers on
(0,1)x =

zeros(1,
3) x =
0

x = ones(1,3)
x =x = 1 1 1
rand(M,N) MxN matrix of uniformly
distributed random

x =
rand(1,3)

x =

0.9501 0.2311 0.606

For example,
>> zeros(2,2) ans =
0 0 0 0
>> ones(4,3) ans =ans =
111111 111111
1

5
16

StandardArrays
eye(2)
ans =
1

00

eye(2,3)
ans =
1

00

Some useful commands:


x = start:end

x = start:increment:end
linspace(start,end,number)
length(x)
y = x

dot (x, y)create row vector x starting with start,


counting by one, ending at end
create row vector x starting with start, counting by
increment, ending at or before end
create row vector x starting with start, ending at end,
having number elements
returns the length of vector x
transpose of vector x
returns the scalar dot product of the vector x and y

Plots
x = 1:2:50; y = x.^2; plot(x,y)

Plots
plot(x,y,'*-') xlabel('Values of x') ylabel('y')

Example
Generate and plot a cosine function
x = [0:0.01:2*pi];
y = cos(x);
plot(x,y)plot(x,y)
2

Example
Adding titles to graphs and axis
title(this is the title)
xlabel(x)
ylabel(y)ylabel(y)
2

-x/3Plot

the function esin(x)


between 0 x4
Create an x-array of 100 samples between
0 and 4 .

>>x=linspace(0,4*pi,100);

Calculate sin(.) of the x-array Calculate


sin(.) of the x-arra
y>>y=sin(x);
>>y1=exp(-x/3);of the xarra
Calculate e-x/3

y Multiply the arrays y and y1


>>y2=y*y1

-x/3Plot

the function esin(x)


between 0 x4

Multiply the arrays y and y1 correctly


>>y2=y.*y1;

Plot the y2-array

0.

>>plot(y
2)0.6

0.2

0.5
0.4
0.3

0.1
0
-0.1
-0.2
0 10 20 30 40 50 60 70 80 90 100 -0.

CREATING A PLOT OF A
FUNCTION111112

A PLOT OF A FUNCTION

4
2for)
6cos(
5.3x
xy11
111
5.0x

Functions

MATLAB
many operations
built in functions
a variety
of has
statistical

which make it easy to perform


sum Sums the content of the variable passed
prod Multiplies the content of the variable passed
mean Calculates the mean of the variable passed
sqrt Calculates the square root of the variable
passed max Finds the maximum of the data max
Finds the maximum of the data
min Finds the minimum of the data
size Gives the size of the variable passed

Special functions
There are a number of special functions that
provide useful constants

pi = 3.14159265.
ior j = square root of -1
Inf = infinity Inf = infinity
NaN = not a numbe

Functions
Passing a vector to a function like sum, mean,
will calculate the property within the vector

>>
sum([1,2,3,4,5])
= 15
>>
mean([1,2,3,4,5])
=3
2
9

S-ar putea să vă placă și