Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Course Name: MATLAB Course Code: CST-439

Name: Sachin Kumar UID: 19CBS1003


Branch/ Section: CSBS / A Date: September 29, 2022

Assignment -1

1. Explain MATLAB API (Application Program Interface)?

MATLAB is a high-performance language for technical computing. It


integrates computation, visualization, and programming in an easy-to-use
environment where problems and solutions are expressed in familiar
mathematical notation.
Typical uses include:
● Math and computation
● Algorithm development
● Modeling, simulation, and prototyping
● Data analysis, exploration, and visualization
● Scientific and engineering graphics
● Application development, including Graphical User Interface building
MATLAB is an interactive system whose basic data element is an array that
does not require dimensioning.
MatLab API is a library that enables you to write Fortran and C programs
that interact with MatLab. It contains the facilities for calling routines from
MatLab, for reading and writing Mat files and calling Matlab as a
computational engine.

2. Can Multi-dimensional arrays support in MATLAB?

Yes MATLAB can support Multi-dimensional arrays. A multidimensional


array in MATLAB® is an array with more than two dimensions. In a matrix,
the two dimensions are represented by rows and columns. Each element is
defined by two subscripts, the row index and the column index.
Multidimensional arrays are an extension of 2-D matrices and use additional
subscripts for indexing. A 3-D array, for example, uses three subscripts. The
first two are just like a matrix, but the third dimension represents pages or
sheets of elements.

3. What are the basic Plots and Graphs of MATLAB?

Basic Plots and graphs of MATLAB are:


● Box: Axis box for 2-D and 3-D plots
● ErrorBar: Plot graph with error bars
● Hold: Hold current graph
● LineSpec: Line specification syntax
● Loglog: Plot using log-log scales
● Polar: Polar coordinate plot
● Plot: Plot vectors or matrices.
● Plot3: Plot lines and points in 3-D space
● Plotyy: Plot graphs with Y tick labels on the left and right
● Semilogx: Semi-log scale plot
● Semilogy: Semi-log scale plot
● Subplot: Create axes in tiled positions

4. Discuss the following basic and commonly used matrix


operations −

a. Addition and Subtraction of Matrices

You can add or subtract matrices. Both the operand matrices must
have the same number of rows and columns.
Create a script file with the following code −
a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c=a+b
d=a-b
The output Displayed for the above code will be :
c=
8 7 9
6 5 14
12 15 10
d=
-6 -3 -3
2 5 -2
2 1 8

b. Division of Matrices

You can divide two matrices using left (\) or right (/) division
operators. Both the operand matrices must have the same number of
rows and columns.
Create a script file with the following code −
a = [ 1 2 3 ; 4 5 6; 7 8 9];
b = [ 7 5 6 ; 2 0 8; 5 7 1];
c=a/b
d=a\b
The output Displayed for the above code will be :
c=
-0.52542 0.68644 0.66102
-0.42373 0.94068 1.01695
-0.32203 1.19492 1.37288

d=
-3.27778 -1.05556 -4.86111
-0.11111 0.11111 -0.27778
3.05556 1.27778 4.30556
c. Scalar Operations of Matrices

When you add, subtract, multiply or divide a matrix by a number, this


is called the scalar operation.
Scalar operations produce a new matrix with the same number of rows
and columns with each element of the original matrix added to,
subtracted from, multiplied by or divided by the number.

Create a script file with the following code −


a = [ 10 12 23 ; 14 8 6; 27 8 9];
b = 2;
c=a+b
d=a-b
e=a*b
f=a/b
The output Displayed for the above code will be :
c=
12 14 25
16 10 8
29 10 11
d=
8 10 21
12 6 4
25 6 7
e=
20 24 46
28 16 12
54 16 18
f=
5.0000 6.0000 11.5000
7.0000 4.0000 3.0000
13.5000 4.0000 4.5000
d. Transpose of a Matrix

The transpose operation switches the rows and columns in a matrix. It


is represented by a single quote(').
Create a script file with the following code −
a = [ 10 12 23 ; 14 8 6; 27 8 9]
b = a'
The output Displayed for the above code will be :
a=
10 12 23
14 8 6
27 8 9
b=
10 14 27
12 8 8
23 6 9

5. Explain two ways to refer to the elements of a cell array



a. Enclosing the indices in first bracket (), to refer to sets of cells

Cell Indexing with Smooth Parentheses, ()


Cell array indices in smooth parentheses refer to sets of cells. For
example, to create a 2-by-2 cell array that is a subset of C, use smooth
parentheses.
upperLeft = C(1:2,1:2)

upperLeft=2×2 cell array


{'one'} {'two'}

{[ 1]} {[ 2]}

Update sets of cells by replacing them with the same number of cells.
For example, replace cells in the first row of C with an equivalent-sized
(1-by-3) cell array.
C(1,1:3) = {'first','second','third'}

C=2×3 cell array

{'first'} {'second'} {'third'}

{[ 1]} {[ 2]} {[ 3]}

If cells in your array contain numeric data, you can convert the cells to
a numeric array using the cell2mat function.
numericCells = C(2,1:3)

numericCells=1×3 cell array

{[1]} {[2]} {[3]}

numericVector = cell2mat(numericCells)

numericVector = 1×3

1 2 3

numericCells is a 1-by-3 cell array, but numericVector is a 1-by-3 array


of type double.

b. Enclosing the indices in braces {}, to refer to the data within


individual cells

Content Indexing with Curly Braces, {}


Access the contents of cells--the numbers, text, or other data within the
cells--by indexing with curly braces.
For example, to access the contents of the last cell of C, use curly
braces.
last = C{2,3}

last = 3

last is a numeric variable of type double, because the cell contains a


double value.
Similarly, you can index with curly braces to replace the contents of a
cell.
C{2,3} = 300

C=2×3 cell array

{'first'} {'second'} {'third'}

{[ 1]} {[ 2]} {[ 300]}

You can access the contents of multiple cells by indexing with curly
braces. MATLAB® returns the contents of the cells as a comma-
separated list. Because each cell can contain a different type of data,
you cannot assign this list to a single variable. However, you can assign
the list to the same number of variables as cells. MATLAB® assigns to
the variables in column order.
Assign contents of four cells of C to four variables.
[r1c1, r2c1, r1c2, r2c2] = C{1:2,1:2}

r1c1 =

'first'

r2c1 = 1

r1c2 =
'second'

r2c2 = 2

If each cell contains the same type of data, you can create a single
variable by applying the array concatenation operator, [], to the comma-
separated list.
Concatenate the contents of the second row into a numeric array.
nums = [C{2,:}]

nums = 1×3

1 2 300

You might also like