Bird
Raised Fist0
MATLABdata~10 mins

Singular value decomposition (svd) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Singular value decomposition (svd)
Start with matrix A
↓
Compute U, S, V such that A = U*S*V'
↓
U: orthogonal matrix (left singular vectors)
↓
S: diagonal matrix (singular values)
↓
V: orthogonal matrix (right singular vectors)
↓
Use U, S, V for analysis or reconstruction
↓
End
Start with matrix A, compute matrices U, S, V so that A equals U times S times V transpose, then use these matrices for further tasks.
Execution Sample
MATLAB
A = [3 1 1; -1 3 1];
[U,S,V] = svd(A);
U
S
V
This code computes the singular value decomposition of matrix A and shows the matrices U, S, and V.
Execution Table
StepActionEvaluationResult
1Define matrix AA = [3 1 1; -1 3 1]A = [3 1 1; -1 3 1]
2Call svd(A)Compute U, S, V such that A = U*S*V'U, S, V matrices computed
3Display UShow left singular vectorsU = [0.71 0.71; 0.71 -0.71] (approx)
4Display SShow singular values on diagonalS = diag([3.87, 2.24]) (approx)
5Display VShow right singular vectorsV = [0.82 0.41 0.41; -0.45 0.89 0; 0.37 0.18 -0.91] (approx)
6Verify reconstructionCalculate U*S*V'Reconstructed A matches original A
7EndNo more stepsExecution complete
💡 All matrices computed and verified, svd process complete
Variable Tracker
VariableStartAfter svdFinal
A[3 1 1; -1 3 1][3 1 1; -1 3 1][3 1 1; -1 3 1]
Uundefined[0.71 0.71; 0.71 -0.71][0.71 0.71; 0.71 -0.71]
Sundefined[3.87 0 0; 0 2.24 0][3.87 0 0; 0 2.24 0]
Vundefined[0.82 0.41 0.41; -0.45 0.89 0; 0.37 0.18 -0.91][0.82 0.41 0.41; -0.45 0.89 0; 0.37 0.18 -0.91]
Key Moments - 3 Insights
Why is matrix S diagonal and not full?
Matrix S contains singular values only on its diagonal, representing the strength of each component. This is shown in execution_table step 4 where S is diagonal with singular values.
Why do we multiply U, S, and V' to get back A?
Because svd decomposes A into U, S, and V such that A = U*S*V'. This is verified in execution_table step 6 where reconstruction matches original A.
Are U and V always square and orthogonal?
Yes, U and V are orthogonal matrices with orthonormal columns, as shown in execution_table steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does matrix S represent?
AMatrix of left singular vectors
BMatrix of right singular vectors
CDiagonal matrix of singular values
DOriginal matrix A
💡 Hint
Refer to execution_table row 4 where S is described as singular values on diagonal
At which step in the execution_table do we verify that U*S*V' reconstructs A?
AStep 3
BStep 6
CStep 5
DStep 2
💡 Hint
Check execution_table row 6 where reconstruction is verified
If matrix A was square and symmetric, what would you expect about U and V?
AU and V would be identical
BU would be zero matrix
CV would be diagonal
DS would be zero matrix
💡 Hint
Recall that for symmetric matrices, U and V are equal (related to eigen decomposition)
Concept Snapshot
Singular value decomposition (svd) splits matrix A into U, S, V such that A = U*S*V'.
U and V are orthogonal matrices with singular vectors.
S is diagonal with singular values.
Use svd to analyze or reconstruct A.
In MATLAB: [U,S,V] = svd(A);
Full Transcript
Singular value decomposition (svd) breaks a matrix A into three matrices: U, S, and V. U and V are orthogonal matrices containing singular vectors, and S is a diagonal matrix with singular values. The product U*S*V' reconstructs the original matrix A. In MATLAB, you use [U,S,V] = svd(A) to compute these matrices. This process helps analyze the structure of A and is useful in many applications like data compression and noise reduction.

Practice

(1/5)
1. What does the MATLAB function svd(A) return when applied to a matrix A?
easy
A. The determinant of matrix A.
B. The inverse of matrix A.
C. Three matrices: U, S, and V such that A = U * S * V'.
D. The transpose of matrix A.

Solution

  1. Step 1: Understand the purpose of svd

    The svd function decomposes a matrix into three parts: U, S, and V.
  2. Step 2: Recall the decomposition formula

    It satisfies the equation A = U * S * V', where U and V are orthogonal matrices and S is diagonal.
  3. Final Answer:

    Three matrices: U, S, and V such that A = U * S * V'. -> Option C
  4. Quick Check:

    svd(A) = U, S, V [OK]
Hint: Remember svd splits matrix into U, S, V parts [OK]
Common Mistakes:
  • Confusing svd with inverse or transpose
  • Expecting a single matrix output
  • Thinking svd returns determinant
2. Which of the following is the correct MATLAB syntax to compute the singular value decomposition of matrix A and store the results in U, S, and V?
easy
A. [U, S, V] = svd(A);
B. svd(U, S, V) = A;
C. U, S, V = svd(A);
D. svd(A, U, S, V);

Solution

  1. Step 1: Recall MATLAB function output syntax

    In MATLAB, multiple outputs are assigned using square brackets on the left side.
  2. Step 2: Match correct svd syntax

    The correct syntax is [U, S, V] = svd(A); to get all three matrices.
  3. Final Answer:

    [U, S, V] = svd(A); -> Option A
  4. Quick Check:

    Use square brackets for multiple outputs [OK]
Hint: Use square brackets for multiple outputs in MATLAB [OK]
Common Mistakes:
  • Using parentheses instead of brackets for outputs
  • Trying to assign outputs on the right side
  • Incorrect argument order in svd
3. Given the MATLAB code:
A = [3 1; 1 3];
[U, S, V] = svd(A);
disp(diag(S));

What will be the output of disp(diag(S))?
medium
A. [0; 0]
B. [3; 1]
C. [1; 3]
D. [4; 2]

Solution

  1. Step 1: Calculate singular values of A

    Matrix A is symmetric. Its singular values are the absolute values of eigenvalues. Eigenvalues of A are 4 and 2.
  2. Step 2: Understand svd output

    The diagonal of S contains singular values in descending order: 4 and 2.
  3. Final Answer:

    [4; 2] -> Option D
  4. Quick Check:

    Singular values = eigenvalues for symmetric matrix [OK]
Hint: For symmetric matrices, singular values = absolute eigenvalues [OK]
Common Mistakes:
  • Confusing singular values with matrix entries
  • Mixing order of singular values
  • Expecting singular values to be zero
4. Consider the MATLAB code snippet:
A = [1 2; 3 4];
[U, S, V] = svd(A);
U = svd(A);

What is the error in this code?
medium
A. Matrix A is not square, so svd cannot be applied.
B. The second call to svd assigns a matrix to U instead of three outputs.
C. The variable V is not assigned in the second svd call.
D. The svd function requires four output variables.

Solution

  1. Step 1: Analyze the first svd call

    The first call correctly assigns three outputs: U, S, V.
  2. Step 2: Analyze the second svd call

    The second call assigns U = svd(A); which returns only singular values as a vector, not a matrix U.
  3. Final Answer:

    The second call to svd assigns a matrix to U instead of three outputs. -> Option B
  4. Quick Check:

    svd(A) alone returns singular values vector [OK]
Hint: svd(A) alone returns singular values vector, not matrices [OK]
Common Mistakes:
  • Expecting svd(A) to return U matrix alone
  • Thinking svd requires square matrix only
  • Ignoring number of outputs in function call
5. You want to approximate a matrix A by keeping only the largest singular value and corresponding vectors using MATLAB. Which code snippet correctly performs this low-rank approximation?
hard
A. [U, S, V] = svd(A);
A_approx = U(:,1) * S(1,1) * V(:,1)';
B. [U, S, V] = svd(A);
A_approx = U(:,1) * V(:,1)';
C. [U, S, V] = svd(A);
A_approx = S(1,1) * V(:,1)';
D. [U, S, V] = svd(A);
A_approx = U(:,1) * S(:,1) * V(:,1)';

Solution

  1. Step 1: Understand low-rank approximation using svd

    Keeping only the largest singular value means using the first column of U, the first singular value, and the first column of V.
  2. Step 2: Construct the approximation

    The approximation is A_approx = U(:,1) * S(1,1) * V(:,1)', multiplying the first singular value and vectors.
  3. Final Answer:

    [U, S, V] = svd(A); A_approx = U(:,1) * S(1,1) * V(:,1)'; -> Option A
  4. Quick Check:

    Low-rank approx = U(:,1)*S(1,1)*V(:,1)' [OK]
Hint: Multiply first U column, singular value, and V column for approximation [OK]
Common Mistakes:
  • Omitting singular value in multiplication
  • Using entire S matrix instead of first singular value
  • Incorrect matrix dimensions in multiplication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard