Bird
Raised Fist0
MATLABdata~20 mins

Filtering basics in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Filtering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple moving average filter
What is the output of the following MATLAB code that applies a moving average filter to a signal?
MATLAB
x = [1 2 3 4 5];
windowSize = 3;
b = (1/windowSize)*ones(1,windowSize);
a = 1;
y = filter(b,a,x);
disp(y);
A[0.3333 1 2 3 4]
B[0 1 2 3 4]
C[1 2 3 4 5]
D[1 2 2 3 4]
Attempts:
2 left
💡 Hint
Think about how the filter function computes each output as a weighted sum of current and previous inputs.
❓ Predict Output
intermediate
2:00remaining
Effect of filter coefficients on output
What is the output of this MATLAB code that applies a filter with coefficients b and a to the input vector x?
MATLAB
x = [0 1 0 0 0];
b = [1 1];
a = 1;
y = filter(b,a,x);
disp(y);
A[0 1 1 0 0]
B[1 1 0 0 0]
C[0 1 0 0 0]
D[1 0 0 0 0]
Attempts:
2 left
💡 Hint
The filter output at each step depends on current and previous inputs weighted by b coefficients.
🔧 Debug
advanced
2:00remaining
Identify the error in filter usage
What error does this MATLAB code produce when run?
MATLAB
x = [1 2 3 4];
b = [1 2 3];
a = [1 0];
y = filter(b,a,x);
AError: 'a' must be a scalar or vector with length equal to or less than 'b'
BError: Inputs must be vectors of the same length
CNo error, outputs filtered vector
DError: Filter denominator coefficients 'a' cannot have zeros except the first element
Attempts:
2 left
💡 Hint
Check the denominator coefficients 'a' for invalid values.
🧠 Conceptual
advanced
2:00remaining
Understanding filter initial conditions
What is the effect of specifying initial conditions in the MATLAB filter function?
AIt changes the filter coefficients permanently
BIt sets the starting state of the filter to avoid transient effects
CIt resets the input vector to zeros before filtering
DIt has no effect on the filter output
Attempts:
2 left
💡 Hint
Think about how filters remember past inputs or outputs.
❓ Predict Output
expert
2:00remaining
Output length after filtering with different coefficient lengths
What is the length of the output vector y after running this MATLAB code?
MATLAB
x = 1:5;
b = [1 -1 1];
a = 1;
y = filter(b,a,x);
A7
B6
C3
D5
Attempts:
2 left
💡 Hint
The filter function output length matches the input length.

Practice

(1/5)
1. What does filtering an array in MATLAB mean?
filtered = array(condition);
easy
A. Selecting elements that meet a condition
B. Sorting the array elements
C. Changing the array size randomly
D. Adding new elements to the array

Solution

  1. Step 1: Understand filtering concept

    Filtering means choosing only those elements from an array that satisfy a condition.
  2. Step 2: Analyze the MATLAB syntax

    The syntax array(condition) returns elements where the condition is true.
  3. Final Answer:

    Selecting elements that meet a condition -> Option A
  4. Quick Check:

    Filtering = Selecting elements [OK]
Hint: Filtering picks elements matching condition inside parentheses [OK]
Common Mistakes:
  • Thinking filtering sorts the array
  • Believing filtering changes array size randomly
  • Confusing filtering with adding elements
2. Which of the following is the correct MATLAB syntax to filter array A for values greater than 10?
easy
A. filtered = A > 10;
B. filtered = A{A > 10};
C. filtered = A(A > 10);
D. filtered = filter(A > 10);

Solution

  1. Step 1: Identify correct filtering syntax

    In MATLAB, filtering uses parentheses with a logical condition inside to select elements.
  2. Step 2: Check each option

    filtered = A(A > 10); uses A(A > 10), which correctly filters elements greater than 10.
  3. Final Answer:

    filtered = A(A > 10); -> Option C
  4. Quick Check:

    Use parentheses with condition for filtering [OK]
Hint: Use parentheses with condition to filter arrays [OK]
Common Mistakes:
  • Using > without parentheses returns logical array, not filtered values
  • Using curly braces {} which are for cell arrays
  • Using undefined function 'filter'
3. What is the output of this MATLAB code?
A = [3, 12, 7, 15, 9];
filtered = A(A >= 10);
disp(filtered);
medium
A. Error
B. [3 7 9]
C. [3 12 7 15 9]
D. [12 15]

Solution

  1. Step 1: Understand filtering condition

    The condition A >= 10 selects elements in A that are 10 or greater.
  2. Step 2: Apply condition to array A

    Elements 12 and 15 satisfy this condition, so filtered = [12 15].
  3. Final Answer:

    [12 15] -> Option D
  4. Quick Check:

    Elements >= 10 = [12 15] [OK]
Hint: Filter keeps elements where condition is true [OK]
Common Mistakes:
  • Including elements less than 10
  • Expecting original array output
  • Confusing logical array with filtered values
4. Identify the error in this MATLAB code for filtering values less than 5:
A = [2, 8, 4, 6];
filtered = A{A < 5};
medium
A. Using curly braces {} instead of parentheses ()
B. Missing semicolon at end of line
C. Incorrect comparison operator
D. Array A is not defined

Solution

  1. Step 1: Check filtering syntax

    Filtering arrays uses parentheses (), not curly braces {} which are for cell arrays.
  2. Step 2: Identify the error

    Using A{A < 5} causes an error because curly braces cannot index numeric arrays.
  3. Final Answer:

    Using curly braces {} instead of parentheses () -> Option A
  4. Quick Check:

    Use () for filtering numeric arrays [OK]
Hint: Use parentheses () to filter numeric arrays, not curly braces {} [OK]
Common Mistakes:
  • Using {} for numeric array indexing
  • Ignoring MATLAB error messages
  • Confusing semicolon necessity
5. Given a matrix M = [1 5 9; 4 2 7; 8 3 6], how do you filter all elements greater than 4 and return them as a single column vector?
hard
A. filtered = filter(M > 4);
B. filtered = M(M > 4); filtered = filtered(:);
C. filtered = M{M > 4};
D. filtered = M(:, M > 4);

Solution

  1. Step 1: Apply filtering condition

    Use M(M > 4) to select all elements greater than 4 from matrix M.
  2. Step 2: Convert result to column vector

    Use filtered(:) to reshape the filtered elements into a single column vector.
  3. Final Answer:

    filtered = M(M > 4); filtered = filtered(:); -> Option B
  4. Quick Check:

    Filter then reshape to column vector [OK]
Hint: Filter with condition, then reshape using (: ) for column vector [OK]
Common Mistakes:
  • Using curly braces {} for filtering
  • Trying to filter columns instead of elements
  • Using undefined function 'filter'
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