Type checking (class, isa) in MATLAB - Time & Space Complexity
We want to understand how long it takes to check the type of a variable in MATLAB.
How does the time needed change when we check many variables or objects?
Analyze the time complexity of the following code snippet.
for i = 1:n
if isa(data{i}, 'double')
disp('Number')
elseif isa(data{i}, 'char')
disp('Text')
else
disp('Other')
end
end
This code checks the type of each element in a cell array and prints a message based on its type.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runsisachecks on each element. - How many times: The loop runs
ntimes, once per element in the input array.
Each new element adds one more type check, so the total work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks |
| 100 | 100 type checks |
| 1000 | 1000 type checks |
Pattern observation: The work grows directly in proportion to the number of elements.
Time Complexity: O(n)
This means the time to check types grows in a straight line as the number of elements increases.
[X] Wrong: "Type checking is instant and does not depend on how many items we check."
[OK] Correct: Each item requires a separate check, so more items mean more time spent.
Knowing how type checks scale helps you write efficient code when handling many variables or objects.
"What if we replaced the for loop with a vectorized function that checks all types at once? How would the time complexity change?"
Practice
class function in MATLAB return when used on a variable?Solution
Step 1: Understand the purpose of
Theclassclassfunction returns the exact type name of the variable, such as 'double', 'char', or a user-defined class. Other choices describe checks for numeric type, size, or value, butclassspecifically returns the type name as a string.Final Answer:
The exact type name of the variable -> Option BQuick Check:
class = exact type name [OK]
- Confusing class with isa function
- Thinking class returns size or value
- Assuming class returns a boolean
x is of class double using isa?Solution
Step 1: Recall
The correct syntax isisafunction syntaxisa(variable, 'classname'), so to check ifxis a double, useisa(x, 'double'). Usingclass(x) == 'double'compares strings unreliably,isa('double', x)reverses arguments, andclass(x, 'double')has invalid syntax.Final Answer:
isa(x, 'double') -> Option CQuick Check:
isa(variable, 'class') syntax [OK]
- Swapping arguments in isa
- Using == to compare class strings
- Calling class with two arguments
x = 5; y = isa(x, 'double'); disp(y);
Solution
Step 1: Trace code execution
Variablexis assigned 5, which defaults to classdoublein MATLAB.isa(x, 'double')returns logical true (displayed as 1), anddisp(y)outputs 1.Final Answer:
1 -> Option DQuick Check:
isa(5, 'double') = 1 [OK]
- Expecting 0 because 5 looks like int
- Confusing output with class function
- Thinking isa returns string
if isa('double', x)
disp('x is double')
endSolution
Step 1: Check
The correct order isisafunction argument orderisa(variable, 'classname'), but here arguments are reversed toisa('double', x). A missing semicolon afterdispor usingisainifare not errors.Final Answer:
Arguments of isa are reversed -> Option AQuick Check:
isa(variable, 'class') order matters [OK]
- Swapping arguments in isa
- Thinking missing semicolon causes error
- Believing isa can't be used in if
isa to check the input type?Solution
Step 1: Identify correct class names and syntax
In MATLAB, numeric arrays usually have class'double', not'numeric'or'array'.if isa(input, 'double') || isa(input, 'table')correctly checks either using properisasyntax and logical OR.Final Answer:
if isa(input, 'double') || isa(input, 'table') -> Option AQuick Check:
Use isa with correct class names [OK]
- Using 'numeric' instead of 'double'
- Using class() with == for strings
- Assuming 'array' is a class name
Ready to prove it?
Pass the test and this pattern moves unknown
