Bird
Raised Fist0
MATLABdata~5 mins

Special matrices (zeros, ones, eye, rand) in MATLAB - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the zeros(n) function do in MATLAB?
It creates an n-by-n matrix filled with zeros. For example, zeros(3) makes a 3x3 matrix where every element is 0.
Click to reveal answer
beginner
How do you create a matrix of all ones with 2 rows and 4 columns in MATLAB?
Use ones(2,4). This creates a 2x4 matrix where every element is 1.
Click to reveal answer
beginner
What is the purpose of the eye(n) function in MATLAB?
It creates an identity matrix of size n-by-n. This matrix has 1s on the diagonal and 0s elsewhere.
Click to reveal answer
beginner
What does the rand(m,n) function generate in MATLAB?
It creates an m-by-n matrix filled with random numbers between 0 and 1. Each number is different every time you run it.
Click to reveal answer
intermediate
How can you create a 4x4 identity matrix and then multiply it by 5 in MATLAB?
First create the identity matrix with eye(4), then multiply by 5: 5 * eye(4). This results in a 4x4 matrix with 5s on the diagonal and 0s elsewhere.
Click to reveal answer
What will zeros(2,3) create?
AA 2x3 matrix of zeros
BA 3x2 matrix of zeros
CA 2x3 matrix of ones
DA 3x3 identity matrix
Which function creates an identity matrix in MATLAB?
Aones()
Beye()
Crand()
Dzeros()
What range of values does rand(3,3) generate?
AOnly 0s and 1s
BRandom integers between 0 and 10
CRandom decimal numbers between 0 and 1
DNegative and positive random numbers
How do you create a 5x5 matrix of all ones?
Azeros(5,5)
Brand(5,5)
Ceye(5)
Dones(5,5)
What does eye(3)*2 produce?
AA 3x3 matrix with 2s on the diagonal and 0s elsewhere
BA 3x3 matrix of twos
CA 3x3 matrix of zeros
DA 3x3 matrix of random numbers
Explain how to create special matrices in MATLAB using zeros, ones, eye, and rand functions.
Think about what each function fills the matrix with.
You got /4 concepts.
    Describe a real-life situation where you might use an identity matrix created by eye(n) in MATLAB.
    Think about how multiplying by 1 keeps a number the same.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which MATLAB function creates a matrix filled entirely with zeros?
      easy
      A. zeros
      B. ones
      C. eye
      D. rand

      Solution

      1. Step 1: Understand the function purpose

        The zeros function creates a matrix where every element is zero.
      2. Step 2: Compare with other functions

        ones creates all ones, eye creates identity matrix, rand creates random values.
      3. Final Answer:

        zeros -> Option A
      4. Quick Check:

        zeros = zeros matrix [OK]
      Hint: Zeros means all elements are zero, so use zeros() [OK]
      Common Mistakes:
      • Confusing zeros with ones
      • Thinking eye creates zeros
      • Using rand for zeros
      2. Which of the following is the correct syntax to create a 3x3 identity matrix in MATLAB?
      easy
      A. eye(3)
      B. ones(3,3)
      C. zeros(3,3)
      D. rand(3)

      Solution

      1. Step 1: Recall identity matrix creation

        The eye(n) function creates an n-by-n identity matrix with ones on the diagonal.
      2. Step 2: Check syntax correctness

        eye(3) correctly creates a 3x3 identity matrix. Others create different matrices.
      3. Final Answer:

        eye(3) -> Option A
      4. Quick Check:

        eye(3) = 3x3 identity [OK]
      Hint: Identity matrix uses eye(n) syntax [OK]
      Common Mistakes:
      • Using ones or zeros instead of eye
      • Missing parentheses
      • Using rand for identity
      3. What is the output of the following MATLAB code?
      A = ones(2,3);
      B = zeros(2,3);
      C = A + B;
      medium
      A. [[2 2 2]; [2 2 2]]
      B. [[0 0 0]; [0 0 0]]
      C. [[1 1 1]; [1 1 1]]
      D. Error due to size mismatch

      Solution

      1. Step 1: Understand matrix creation

        ones(2,3) creates a 2x3 matrix of all ones, zeros(2,3) creates a 2x3 matrix of zeros.
      2. Step 2: Add matrices element-wise

        Adding ones and zeros element-wise results in the same ones matrix.
      3. Final Answer:

        [[1 1 1]; [1 1 1]] -> Option C
      4. Quick Check:

        ones + zeros = ones [OK]
      Hint: Adding zeros keeps original values unchanged [OK]
      Common Mistakes:
      • Thinking addition doubles values
      • Confusing matrix sizes
      • Expecting error on addition
      4. Identify the error in this MATLAB code:
      M = zeros(3);
      M(2,4) = 5;
      medium
      A. Matrix must be created with ones, not zeros
      B. Index exceeds matrix dimensions error
      C. No error, code runs fine
      D. Syntax error due to missing semicolon

      Solution

      1. Step 1: Check matrix size

        zeros(3) creates a 3x3 matrix, so valid indices are 1 to 3 for both rows and columns.
      2. Step 2: Analyze indexing

        Accessing M(2,4) tries to access column 4, which does not exist, causing an index error.
      3. Final Answer:

        Index exceeds matrix dimensions error -> Option B
      4. Quick Check:

        Index out of range = error [OK]
      Hint: Check matrix size before indexing [OK]
      Common Mistakes:
      • Ignoring matrix size limits
      • Thinking semicolon causes error
      • Assuming zeros() creates bigger matrix
      5. You want to create a 4x4 matrix with ones on the diagonal and random numbers between 0 and 1 elsewhere. Which MATLAB code achieves this?
      hard
      A. M = rand(4); M(eye(4) == 0) = 1;
      B. M = eye(4) + rand(4) - diag(rand(4,1));
      C. M = ones(4); M(eye(4) == 0) = rand(4);
      D. M = rand(4); M(eye(4) == 1) = 1;

      Solution

      1. Step 1: Understand the goal

        Create a 4x4 matrix with random values, then set diagonal elements to 1.
      2. Step 2: Analyze each option

        M = rand(4); M(eye(4) == 1) = 1; creates random 4x4 matrix, then replaces diagonal elements (where eye(4)==1) with 1, matching the requirement.
      3. Final Answer:

        M = rand(4); M(eye(4) == 1) = 1; -> Option D
      4. Quick Check:

        Random matrix with diagonal ones = M = rand(4); M(eye(4) == 1) = 1; [OK]
      Hint: Set diagonal after random matrix creation using logical indexing [OK]
      Common Mistakes:
      • Adding matrices incorrectly
      • Replacing wrong elements
      • Using ones() instead of random values
      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