0% found this document useful (0 votes)
44 views15 pages

Matlab Lecture 3

This document discusses creating and manipulating arrays in MATLAB. It covers creating one-dimensional vectors and two-dimensional matrices, addressing elements, adding and deleting elements, built-in functions like length(), size(), reshape(), and diag(), and using strings as variables. The chapter concludes with practice problems for the concepts covered.

Uploaded by

waqas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views15 pages

Matlab Lecture 3

This document discusses creating and manipulating arrays in MATLAB. It covers creating one-dimensional vectors and two-dimensional matrices, addressing elements, adding and deleting elements, built-in functions like length(), size(), reshape(), and diag(), and using strings as variables. The chapter concludes with practice problems for the concepts covered.

Uploaded by

waqas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 3

Chapter 2 Creating Arrays


• Creating Arrays

• Matrix
• One dimensional (Row/Column) - Vectors
• Two dimensional (Rows/Columns) - Matrix
Chapter 2 One dimensional Array (Vector)
• Creating Arrays
• One Dimensional Array

Creating Vector by (Know list of #’s)


Variable_Name = [Vector Elements]
Row Vector
Type elements by giving Space
Column Vector
Type elements by giving ; or Enter Key
Chapter 2 One dimensional Array (Vector)
• Creating Arrays
• One Dimensional Array

Creating Vector with constant Spacing

Variable_Name =[m:q:n]

Brackets are optional


Default Spacing is 1
Chapter 2 One dimensional Array (Vector)
• Creating Arrays
• One Dimensional Array

Creating Vectors with number of terms

Variable_Name = linspace(xi,xf,n)

By default number of terms are 100


Chapter 2 Two dimensional Array (Matrix)
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array

Type row element by placing ; or by hitting Enter key

Variable_Name = [1st Row; 2nd Row ;…; Last Row]

• All Rows must have same numbers/elements


• Elements can be numbers/combination of variables
• You can use spacing commands (previously discussed) also
Chapter 2 Two dimensional Array (Matrix)
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
Zeros, Ones and eye command

• zeros(m,n) (Zero matrix)


• ones(m,n) (ones matrix)
• eye(n) (Identity matrix)
Chapter 2 Variables in MATLAB
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• All variables in MATLAB are arrays
• Scaler with one element
• Vectors with Row/column
• Matrix with Rows/Columns

They can be changed with other size.


Chapter 2 Transpose Operator
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• Transpose Operator

Transpose Operator (Single quote)


Chapter 2 Array Addressing
• Creating Arrays
Vector addressing
• One Dimensional Array Variable_Name(k) (k is the place)
• Two Dimensional Array To change the specific value
• Variables in MATLAB Variable_Name(k) = Value
• Transpose Operator
Matrix addressing
• Array Addressing Variable_Name(k,p) (k for Rows P for columns)
To change the specific Value
Variable_Name(k,p) = Value
Array addressing (using colon)
Vector addressing
Variable_Name(:) for all elements
Variable_Name(m:n) for m to n elements
Matrix addressing
A(:,n) for one column
A(:,m:n) for range of columns
A(n,:) for one row
A(m:n,:) for range of rows
A (m:n,P:q) for range of rows and columns
Chapter 2 Adding Elements to Existing Variables
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array Into Vector
• Variables in MATLAB Variable_Name(k)=value (k is location)
• Transpose Operator
• Array Addressing Into Matrix
• Adding elements to Variable_Name(k)=value (k is location)
existing variables

• If the element is not on the instant place then zeros placed


by defaultly through MATLAB
• You can use range commands
Chapter 2 Deleting Elements
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• Transpose Operator
• Array Addressing
• Adding elements to
existing variables
• Deleting Elements Variable_Name(k)=[]
Chapter 2 Built-in Functions for Arrays
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• Transpose Operator
• Array Addressing
length(A) Total number of elements in array
• Adding elements to
size(A) order of array
existing variables
reshape(A,m,n) Reshape matrix column wise
• Deleting Elements
diag(v) to make diagonal matrix
• Built-in Functions
diag(A) to extract the diagonals
Chapter 2 Strings as Variables
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• Transpose Operator
• Array Addressing
• Adding elements to Single quote use for print string
existing variables You can use commands to extract or replace string part
• Deleting Elements You can print multi strings as
• Built-in Functions Variable_Name=char(‘string1’,’string2’,’string3’)
• Strings as Variables
Chapter 2 Practice and Problems
• Creating Arrays
• One Dimensional Array
• Two Dimensional Array
• Variables in MATLAB
• Transpose Operator
• Array Addressing
• Adding elements to
existing variables
• Deleting Elements Practice and solve Problems of exercise
• Built-in Functions
• Strings as Variables
• Practice and Problems

You might also like