Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

 Introduction

 

The slides cover the following topics:

1. Vectors and Matrices

2. Matrix Calculations

3. Loops

4. for Loop

5. while Loop

6. Logical Operations

7. if statement

8. if-else statement

9. Functions in Matlab

10. Plotting in Matlab

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Vectors and Matrices

 

•In MATLAB a vector can be defined as row vector or as a column vector.
 
•A vector of length n can be visualized as matrix of size 1xn or nx1.
Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Vectors and Matrices

 

•Matrices can be of different sizes, either square matrices of size NxN or rectangular NxM.
 
•Note that vectors are special case of matrices.

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Some Special Vectors and Matrices

 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Matrix Calculations

 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Solving Simultaneous Equations Ax=b

•A=matrix of coefficients,
 
•x = vector of variables,
 
•b = vector of values.

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Loops

 

•Two different types of loops exist in MATLAB.
 
  • for   and
  • while
 
•Their structure is as shown here
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

for loop

 

•A simple for loop to calculate and display squares of numbers from 1 to 5.

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

for loop (contd.)

 

•We can write the same for loop with some change. Now we are saving the results in a vector named ‘result’.
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

for loop (contd.)

 

•The control index of the for loop shown in the previous slides was incremented by ‘1’. We can increment the control index by any arbitrary value as required.
•In the following example odd numbers from 1 to 9 are printed.
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

while loop

 

•While loop continue to execute if the given condition is true.
 
•A condition is true if it is not equal to ‘0’.
 
•A simple while loop to display numbers from 1 to 10 is shown here.

 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Logical Operators

 

•AND, OR, and INVERT logical operations can be performed in MATLAB.
 
•The symbols used for these operators are ‘&&’ , ‘||’, and ‘~’ respectively for AND, OR, and INVERT.
 
•A logical operation always results in a ‘1’ or ‘0’.

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Some more logical operators

 

•Other logical operators in MATLAB include
 
 
 
Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)
•Note that the results of these operations are also either ‘1’ or ‘0’.
 
Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide
 
Examples: logical operations
 
•Lets say, a=1, b=2, and c=1. Then,
 
•A<b results in TRUE I.e. ‘1’
 
•A>b results in FALSE I.e. ‘0’
 
•A==b results in FALSE I.e. ‘0’
 
•A==c results in TRUE I.e. ‘1’
 
•A==c && a<=b results in ‘1’.
 
 
•Several examples can be used to show the usage of these operators.
 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

while loop revisited

 

•Now we can show the usage of these logical operations in the while loop as condition.
 
•Say a=1, then the following code results in the execution of loop until the condition a<b gets FALSE.
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

if statement

 

if statement checks whether the condition given to it is TRUE or FALSE.
 
Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)
 
•If the condition evaluates to TRUE the <statements> are executed otherwise not.
 
•Unlike the for and while loops the <statements> inside the if statement are executed only once.
 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

if-else statement

 

•If-else is used if we want to perform two different tasks depending upon the condition is TRUE or FALSE.
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

We wish to use “if” and “else” statement to decide upon if the input integer is an odd or an even number. The Matlab code for the purpose is shown below:

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Functions in Matlab

•Functions are actually computer routines(programs) to perform specific task.
 
•Matlab is rich in built-in function
 
•A function is called, user gives specific input(s) to the function, which generates the required output(s).
 
 
Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide
 
 
Example of built-in functions
 
•Our objective is to calculate mean or average of certain vector ‘x’.
 
•We call the built in function “mean” of Matlab and give ‘x’ as an input to the function.
 
•The mean is calculated and is stored in variable ‘y’.

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)  Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

•We can also built our own functions and call them when required.
 
•The functions are the m-files.
 
•For example for a given quadratic equation we wish to calculate the roots of equation. So we build our own function “call” and use the quadratic formula to solve for x1 and x2.
 
Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)
 
Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide
 
 
•In our main program the function is called and the inputs ‘a’, ‘b’ and ‘c’ are given to the function which calculates the required roots
 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Plotting in Matlab

 

•Matlab uses “plot” command to plot the curves with different line style and colors
 
•Example follows:

 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

 

Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

 

Matlab Vectors and Matrices and Basic Instructions---------------------------Next slide

The document Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

FAQs on Chapter - Matlab Vectors and Matrices and Basic Instructions, PPT, ADC, Semester, Engineering - Computer Science Engineering (CSE)

1. What is the difference between a vector and a matrix in MATLAB?
Ans. In MATLAB, a vector is a one-dimensional array that can hold a sequence of values, while a matrix is a two-dimensional array that can hold values arranged in rows and columns. Vectors can be represented as either row vectors or column vectors in MATLAB.
2. How can I create a vector of evenly spaced values in MATLAB?
Ans. MATLAB provides the colon operator (:) to create a vector of evenly spaced values. For example, the expression "1:5" will create a vector with values [1, 2, 3, 4, 5].
3. Can I perform arithmetic operations on vectors and matrices in MATLAB?
Ans. Yes, MATLAB allows you to perform arithmetic operations on vectors and matrices. You can add, subtract, multiply, and divide vectors and matrices element-wise using the appropriate operators (+, -, *, /). MATLAB also provides functions for matrix multiplication and other advanced operations.
4. How can I access specific elements or subarrays of a matrix in MATLAB?
Ans. In MATLAB, you can access specific elements of a matrix using indexing. For example, to access the element in the 2nd row and 3rd column of a matrix A, you can use the expression "A(2,3)". You can also access subarrays or subsets of a matrix using indexing. For example, to access the first two rows of a matrix A, you can use the expression "A(1:2, :)".
5. Can I perform calculations on matrices with different dimensions in MATLAB?
Ans. MATLAB supports matrix operations even when the matrices have different dimensions, as long as the dimensions are compatible. MATLAB automatically applies element-wise operations when the dimensions of the matrices match. If the dimensions are not compatible, MATLAB will display an error. In such cases, you may need to reshape or transpose the matrices to make them compatible for the desired operation.
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

Semester

,

pdf

,

Semester Notes

,

Previous Year Questions with Solutions

,

Sample Paper

,

Extra Questions

,

ADC

,

Chapter - Matlab Vectors and Matrices and Basic Instructions

,

Engineering - Computer Science Engineering (CSE)

,

PPT

,

MCQs

,

past year papers

,

Important questions

,

Free

,

Chapter - Matlab Vectors and Matrices and Basic Instructions

,

ppt

,

ADC

,

mock tests for examination

,

PPT

,

Engineering - Computer Science Engineering (CSE)

,

practice quizzes

,

Summary

,

Semester

,

Viva Questions

,

Semester

,

PPT

,

Chapter - Matlab Vectors and Matrices and Basic Instructions

,

Exam

,

video lectures

,

Objective type Questions

,

study material

,

ADC

,

Engineering - Computer Science Engineering (CSE)

,

shortcuts and tricks

;