MATLAB Module 2: Vectors, Matrices, Array Operations

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

In MATLAB, what is an array?

  • A command used to clear the workspace
  • A function used for plotting graphs
  • A collection of data values organized into rows and columns (correct)
  • A single numerical value

What is a one-dimensional array also known as?

  • Cell
  • Scalar
  • Matrix
  • Vector (correct)

What is a two-dimensional array referred to as?

  • Vector
  • String
  • Matrix (correct)
  • Scalar

How are scalars treated in MATLAB?

<p>As arrays with 1 row and 1 column (A)</p> Signup and view all the answers

In MATLAB, how do you create a row vector?

<p>Separate each number with a comma (,) (C)</p> Signup and view all the answers

How can a vector element be accessed in MATLAB?

<p>Using parentheses () (B)</p> Signup and view all the answers

In MATLAB, at what index does the vector start?

<p>1 (C)</p> Signup and view all the answers

What does the command length(x) return in MATLAB?

<p>The length of vector x (D)</p> Signup and view all the answers

What does the command y = x' do in MATLAB?

<p>Transposes vector x (D)</p> Signup and view all the answers

What does the command dot(x, y) calculate in MATLAB?

<p>The scalar dot product of vectors x and y (B)</p> Signup and view all the answers

What does the command cross(x, y) return in MATLAB?

<p>Return the vector as the cross product of x and y (D)</p> Signup and view all the answers

What does the function randi() do in MATLAB?

<p>Uniformly distribute random integers (B)</p> Signup and view all the answers

In matrix creation, what are used to separate elements in a row?

<p>Spaces or commas (C)</p> Signup and view all the answers

In matrix creation, what are used to separate rows?

<p>Semicolon or enter (D)</p> Signup and view all the answers

What MATLAB command is used to generate a matrix of all zeros?

<p><code>zeros()</code> (D)</p> Signup and view all the answers

What does the command eye(n) do in MATLAB?

<p>Returns an n x n identity matrix (C)</p> Signup and view all the answers

In MATLAB, what does the operator .* do?

<p>Element-by-element multiplication (A)</p> Signup and view all the answers

In MATLAB, what is the function of the operator ./?

<p>Element-by-element division. (B)</p> Signup and view all the answers

What is the equivalent of $B = A^T$ in MATLAB?

<p>B = A' (B)</p> Signup and view all the answers

How can you calculate the inverse of matrix A in MATLAB?

<p>inv(A) (C)</p> Signup and view all the answers

In MATLAB, what does the function eig(x) calculate?

<p>The eigenvalues of x (B)</p> Signup and view all the answers

Flashcards

What is an array?

A collection of data values organized into rows and columns, known by a single name.

What is a vector?

A one-dimensional array of numbers.

What is a matrix?

A two-dimensional array of numbers.

Scalars in MATLAB

Scalars are treated as 1x1 arrays in MATLAB, represented by their numerical value.

Signup and view all the flashcards

Vector Definition

A vector is an array (row or column) of numbers or scalar values.

Signup and view all the flashcards

Row Vector Separator

Comma ", " is used to separate numbers in a row vector.

Signup and view all the flashcards

Column Vector Separator

Semicolon "; " separates numbers in a column vector.

Signup and view all the flashcards

Vector addressing

A vector element can be accessed in MATLAB with an integer index enclosed in parentheses.

Signup and view all the flashcards

Colon Notation

Colon notation accesses a block of elements. (start:increment:end)

Signup and view all the flashcards

x = start:end (MATLAB)

Creates a row vector x starting with 'start', increments by one, and ends at 'end'.

Signup and view all the flashcards

length(x) (MATLAB)

Returns the number of elements in vector x.

Signup and view all the flashcards

y = x' (MATLAB)

Transposes vector x, switching rows to columns or vice-versa.

Signup and view all the flashcards

dot(x, y) (MATLAB)

Returns the scalar dot product of vectors x and y.

Signup and view all the flashcards

sum(x) (MATLAB)

Function to returns the sum of all elements in x

Signup and view all the flashcards

cross(x, y) (MATLAB)

Returns the vector as their cross product.

Signup and view all the flashcards

randi() (MATLAB)

Uniformly distributes random integers.

Signup and view all the flashcards

rand() (MATLAB)

Generates uniformly distributed array of random numbers between 0 and 1.

Signup and view all the flashcards

What is a Matrix?

A two-dimensional array. Described as i x j where i is the number of rows and j is the number of columns.

Signup and view all the flashcards

Matrix Addressing in MATLAB

Function matrixname(row, column) is used for matrix addressing.

Signup and view all the flashcards

zeros(M, N) (MATLAB)

Creates an MxN matrix filled with zeros.

Signup and view all the flashcards

ones(M, N) (MATLAB)

Creates an MxN matrix filled with ones.

Signup and view all the flashcards

eig(x) (MATLAB)

Finds the eigenvalues of matrix x.

Signup and view all the flashcards

eye(n) function

Identity Matrix: eye(n) returns an n x n identity matrix.

Signup and view all the flashcards

.* operator

Element-by-element multiplication in a matrix.

Signup and view all the flashcards

.^ operator

Element-by-element exponentiation in a matrix.

Signup and view all the flashcards

Study Notes

  • EE 153: Introduction to MATLAB, Module 2 covers vectors, matrices, and array operations.

Learning Objectives

  • Understand the basics of vectors and matrices.
  • Learn matrix operations and array manipulation.
  • Apply element-wise operations and matrix algebra in MATLAB.
  • Use matrix operations for solving linear equations.
  • Practice solving linear equations in power systems using matrix operations.

Array

  • An array is a collection of data values organized into rows and columns, known by a single name.
  • Arrays can be classified as vectors and matrices.
  • A vector is a one-dimensional array.
  • A matrix is a two-dimensional array.
  • Scalars are treated as arrays by MATLAB (1 row and 1 column).
  • Scalars are quantities represented just by their numerical value, such as 3, -5, 0.368.

Vectors

  • A vector is an array of numbers or a list of scalar values in a one-dimensional array of numbers, which can be in a row or column.
  • A = [5 6 7 8 9] is a 1 x 5 array with 5 elements, representing a row vector.
  • Comma "," is used to separate each number for a row vector.
  • >> VR = [6, 7, 8] results in VR = 6 7 8
  • Semicolon ";" is used to separate each number for a column vector.
  • >> VC = [5; 6; 7] results in VC as a column vector with elements 5, 6, and 7.

Vector Addressing

  • Vector Addressing involves accessing a vector element in MATLAB with an integer index enclosed in parentheses.
  • For example, from vector A, >> A(3) results in ans = 7, which is the 3rd element of vector A.
  • Colon notation may be used to address a block of elements, with the syntax (start:increment:end).
  • "start" is the starting index, "increment" is the amount to add to each successive index, and "end" is the ending index.
  • (start: end) is used if the increment is 1.
  • E.g., >> A(1:3) results in ans = 5 6 7, which are the 1st to 3rd elements of vector A.
  • MATLAB index starts at 1.

Some Useful Commands

  • x = start: end creates a row vector x starting with "start," counting by one, and ending at "end."
  • length(x) returns the length of vector x.
  • y = x' transposes vector x.
  • dot(x, y) returns the scalar dot product of the vectors x and y.
  • sum(x) returns the sum of all numbers in x.
  • cross(x, y) returns the vector as the cross product of x and y.
  • randi() uniformly distributes random integers.
  • rand() generates a uniformly distributed array of random numbers between 0 and 1.

Matrices

  • A matrix is a two-dimensional array.
  • A matrix having i rows and j columns is called an i x j matrix.
  • Matrix creation begins with "[" and ends with "]".
  • Spaces or commas are used to separate elements in a row.
  • A semicolon or enter is used to separate rows.
  • Elements are arranged in rows (i) and columns (j).
  • >> f = [ 1 2 3; 4 5 6] creates a matrix f.
  • >> M = [4, 8, 9, 6; 9, 6, 9, 6; 3, 6, 9, 6] creates a matrix M.

Matrix Addressing

  • The function matrixname(row, column) is used for matrix addressing.
  • For example, >> f(2,3) returns the element in the 2nd row and 3rd column of matrix f.
  • >> h(:,1) selects all rows in the first column of matrix h.
  • A colon may be used in place of a row or column reference to select the entire row or column.
  • >> a = [1, 2, 3; 4, 5, 6] defines matrix a in MATLAB.
  • >> a(1,:) selects row 1 and every column in matrix a.
  • >> a(1, 1:2) selects the first two elements in the first row in matrix a.
  • >> a(:) changes matrix a into a column vector.

Special Matrices/Useful Commands

  • zeros(M,N) creates an MxN matrix of zeros.
    • Example: x = zeros(1,3) results in x = 0 0 0
  • ones(M,N) creates an MxN matrix of ones.
    • Example: x = ones(1,3) results in x = 1 1 1
  • eig(x) finds the eigenvalues of x.
    • Example: x = [1,4;3,2], lambda = eig(x), results in lambda = -2 5

Matrix Operations

  • Transpose: B = A'
  • Identity Matrix: eye(n) returns an n x n identity matrix.
    • eye(m,n) returns an m x n matrix with ones on the main diagonal and zeros elsewhere.
  • Addition and subtraction: C = A + B, C = A - B
  • Scalar Multiplication: B = αA, where α is a scalar
  • Matrix Multiplication: C = A*B
  • Matrix Inverse: B = inv(A). A must be a square matrix; rank(A) returns the rank of the matrix A.
  • Matrix Powers:
    • B = A.^2 squares each element in the matrix.
    • C = A*A computes A*A, and A must be a square matrix.
  • Determinant: det(A), A must be a square matrix.
  • A, B, C are defined as matrices, and m, n, α are scalars.

Operators (Element-wise)

  • .* is the element-by-element multiplication operator.
  • ./ is the element-by-element division operator.
  • .^ is the element-by-element power operator.
  • Example of element-by-element division:
    • If >> a = [1, 2, 12] and >> b = [1, 1, 2]
    • Then >> a ./ b results in: ans = 1 2 6
  • Example of element-by-element multiplication:
    • If >> a = [1, 2, 3] and >> b = [0, 1, 2]
    • Then >> a .* b results in: ans = 0 2 6
  • Example of element-by-element power:
    • If >> a = [1, 2, 3] and >> b = [0, 1, 2]
    • Then >> a .^ b results in: ans = 1 2 9

Practice 1

  • Consider the vector: A = [4, 8, 15, 16, 23, 42]
  • Write MATLAB commands to extract the first three elements of A using colon notation.
  • Write commands to replace the fourth element of A with 100.
  • Write commands to create a new vector B consisting of the last three elements of A.

Practice 2

  • Create a 3×3 matrix M in MATLAB with rows: [1 2 3], [4 5 6], and [7 8 9].
  • Extract the entire second row using proper matrix addressing.
  • Compute a new matrix where each element of M is squared using the element-by-element power operator.
  • Replace the third column of M with the vector [10; 11; 12].

Practice 3

  • Solving Systems of linear equations, like the one below:
    • 3a + 2b - c = 10
    • -a + 3b + 2c = 5
    • a - b - c = -1
  • Formulate the coefficient matrix and constant vector corresponding to the system.
  • Solve the system using the matrix inverse approach.
  • Solve the same system using MATLAB's matrix division operator.
  • Verify that both methods yield the same solution.

Practice 4

  • Modeling of the Kirchhoff's Voltage Law (KVL) states that the sum of all voltages around a closed loop in a circuit must be equal to zero.
  • Write a set of linear equations for a circuit with m loops.
  • Use symbolic math tool box

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Programming for Engineers - Module 1
29 questions
MATLAB Commands Quiz
17 questions

MATLAB Commands Quiz

WealthyCarnelian2058 avatar
WealthyCarnelian2058
Matlab Lektion 1-4
33 questions

Matlab Lektion 1-4

PleasurableSilver6540 avatar
PleasurableSilver6540
Use Quizgecko on...
Browser
Browser