Experiment No 1: AIM: Creating A One-Dimensional Array (Row / Column Vector) Creating A
Experiment No 1: AIM: Creating A One-Dimensional Array (Row / Column Vector) Creating A
THEORY:
Quitting MATLAB: To end your MATLAB session, select File > Exit
MATLAB in the desktop, or type quit in the Command Window. You can run a
script file named finish.m each time MATLAB quits.
The inverse of a matrix does not always exist. If the determinant of the matrix is
zero, then the inverse does not exist and the matrix is singular.
In MATLAB, inverse of a matrix is calculated using the inv function. Inverse of a
matrix A is given by inv(A).
The transpose operation switches the rows and columns in a matrix. It is
represented by a single quote(').
The rank function provides an estimate of the number of linearly independent rows
or columns of a full matrix.
k = rank(A) returns the number of singular values of A that are larger than the
default tolerance, max(size(A))*eps(norm(A)).
k = rank(A,tol) returns the number of singular values of A that are larger than tol.
PROCEDURE:
To plot the graph of a function, you need to take the following steps:
1. Define x, by specifying the range of values for the variable x, for which
the function is to be plotted.
SUGGESTED PROGRAM:
1)
A=[1 2 3 4];
display(A);
B=[5 6 7 8];
display(B);
ADD=A+B;
display(ADD);
SUBTRACT=A-B;
display(SUBTRACT);
MULTIPLY=2*A;
display(MULTIPLY);
DIVISION=B/2;
display(DIVISION);
EXP=exp(A);
display(EXP);
OUTPUT
A=
1 2 3 4
B=
5 6 7 8
ADD =
6 8 10 12
SUBTRACT =
-4 -4 -4 -4
MULTIPLY =
2 4 6 8
DIVISION =
EXP =
b)
X=[1 20 3; 5 6 7; 8 9 10];
display(X);
RANK=rank(X);
display(RANK);
INVERSE=inv(X);
display(INVERSE);
TRANSPOSE=X';
display(TRANSPOSE);
OUTPUT:
X=
1 20 3
5 6 7
8 9 10
RANK =
INVERSE =
TRANSPOSE =
1 5 8
20 6 9
3 7 10
PRECAUTIONS:
THEORY:
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. MATLAB is designed to operate primarily on whole
matrices and arrays. Therefore, operators in MATLAB work both on scalar and
non-scalar data. MATLAB allows the following types of elementary operations:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operations
Set Operations
Arithmetic Operators
MATLAB allows two different types of arithmetic operations:
Matrix arithmetic operations
Operator Description
Addition or unary plus. A+B adds A and B. A and B must have
+ the same size, unless one is a scalar. A scalar can be added to a
matrix of any size.
Subtraction or unary minus. A-B subtracts B from A. A and B
- must have the same size, unless one is a scalar. A scalar can be
subtracted from a matrix of any size.
Matrix multiplication. C = A*B is the linear algebraic product of
the matrices A and B. More precisely,
*
For nonscalar A and B, the number of columns of A must equal
the number of rows of B. A scalar can multiply a matrix of any
size.
Operator Description
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Logical Operators
MATLAB offers two types of logical operators and functions:
Bitwise Operations
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables
for &, |, and ^ are as follows:
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
~A = 1100 0011
MATLAB provides various functions for bit-wise operations like 'bitwise and',
'bitwise or' and 'bitwise not' operations, shift operation, etc.
MATLAB provides various functions for set operations, like union, intersection
and testing for set membership, etc.
SUGGESTED PROGRAM:
>> display(B);
B=
7 8 9
4 5 6
1 2 3
>> C=[A ; B]
C=
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3
>> cat(1,A,B)
ans =
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3
>> cat(2,A,B)
ans =
1 2 3 7 8 9
4 5 6 4 5 6
7 8 9 1 2 3
D= A(2,2)
D=
5
>> F= A(1:3 , 2)
F=
2
5
8
>> F= A(:,2)
F=
2
5
8
>> F= A(2,:)
F=
4 5 6
SORTING
G= sort(B)
G=
1 2 3
4 5 6
7 8 9
>> Z=[2, 1, 5; 1, 4, 2; 7, 8, 1]
Z=
2 1 5
1 4 2
7 8 1
>> G=sort(Z,1)
G=
1 1 1
2 4 2
7 8 5
>> G=sort(Z,2) // sorting row wise asec.
G=
1 2 5
1 2 4
1 7 8
G=
5 2 1
4 2 1
8 7 1
SHIFTING
H=rot90(A)
H=
3 6 9
2 5 8
1 4 7
H=rot90(A,2)
H=
9 8 7
6 5 4
3 2 1
RESHAPING
Q=[1, 2, 3, 4 ; 5, 6, 7 ,8 ; 9, 10, 11, 12]
Q=
1 2 3 4
5 6 7 8
9 10 11 12
>> I=reshape(Q,4,3)
I=
1 6 11
5 10 4
9 3 8
2 7 12
>> I=reshape(Q,6,2)
I=
1 3
5 7
9 11
2 4
6 8
10 12
FLIPPING
J=fliplr(Q) //LEFT RIGHT ROT.
J=
4 3 2 1
8 7 6 5
12 11 10 9
K=
3×3 logical array
1 1 1
0 0 0
0 0 0
>> K=A>B
K=
3×3 logical array
0 0 0
0 0 0
1 1 1
>> K=A<=B
K=
3×3 logical array
1 1 1
1 1 1
0 0 0
>> K=A>=B
K=
3×3 logical array
0 0 0
1 1 1
1 1 1
>> K=A==B
K=
3×3 logical array
0 0 0
1 1 1
0 0 0
>> K=A~=B
K=
3×3 logical array
1 1 1
0 0 0
1 1 1
L=or(A,B)
L=
1 1 1
1 1 1
1 1 1
>> L=and(A,B)
L=
1 1 1
1 1 1
1 1 1
>> L=not(A)
L=
0 0 0
0 0 0
>> L=xor(A,B)
L=
0 0 0
0 0 0
0 0 0
PRECAUTIONS:
THEORY:
When you create random numbers using software, the results are not random in a
strict, mathematical sense. However, software applications, such as MATLAB®,
use algorithms that make your results appear to be random and independent. The
results also pass various statistical tests of randomness and independence. These
apparently random and independent numbers are often described
as pseudorandom and pseudoindependent. You can use these numbers as if they
are truly random and independent. One benefit of using pseudorandom,
pseudoindependent numbers is that you can repeat a random number calculation at
any time. This can be useful in testing or diagnostic situations.
Although repeatability can be useful, it is possible to accidentally repeat your
results when you really want different results. There are several ways to avoid this
problem. The documentation contains several examples that show how to ensure
that your results are different when that is your intention.
All the random number functions, rand, randn, randi, and randperm, draw values
from a shared random number generator. Every time you start MATLAB ®, the
generator resets itself to the same state. Therefore, a command such
as rand(2,2) returns the same result any time you execute it immediately following
startup. Also, any script or function that calls the random number functions returns
the same result whenever you restart.
PROCEDURE:
There are four fundamental random number functions: rand, randi, randn,
and randperm. The rand function returns real numbers between 0 and 1 that are
drawn from a uniform distribution. For example,
r1 = rand(1000,1);
SUGGESTED PROGRAM:
C=rand(3,3)
C=
>> C=randi(2,3)
C=
2 2 1
1 1 1
2 2 2
>> C=randi(2,3,2)
C=
2 1
2 2
2 2
>> C=randn(3,3)
C=
>> C=randperm(5,3)
C=
1 3 4
>> C=randperm(5,3)
C=
5 2 1
>> C=randperm(3)
C=
2 1 3
plot(C);
>>xlabel('rows');
>>ylabel('col');
>>title('random matrix');
B)
A = [1 4 7; 2 5 8; 3 6 9]
A = 3×3
1 4 7
2 5 8
3 6 9
B = cumsum(A)
B = 3×3
1 4 7
3 9 15
6 15 24
A = [1 3 5; 2 4 6];
A = 2×3
1 3 5
2 4 6
B = cumsum(A,2)
B = 2×3
1 4 9
2 6 12
PRECAUTIONS: