Properties and methods in MATLAB - Time & Space Complexity
We want to understand how the time it takes to use properties and methods grows as the program runs.
How does accessing or using these parts affect the program speed when the input changes?
Analyze the time complexity of the following code snippet.
classdef MyClass
properties
Data
end
methods
function obj = MyClass(n)
obj.Data = zeros(1, n);
end
function val = getValue(obj, idx)
val = obj.Data(idx);
end
end
end
obj = MyClass(1000);
value = obj.getValue(500);
This code creates an object with a property holding an array and a method to get a value from that array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing an element from the array property inside the method.
- How many times: Each method call accesses one element, so once per call.
Accessing a property element takes the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to get a value does not grow with the size of the data.
Time Complexity: O(1)
This means accessing a property or calling a simple method takes the same time no matter how big the data is.
[X] Wrong: "Accessing a property inside a method takes longer if the property holds more data."
[OK] Correct: Accessing a single element is direct and does not depend on the total size of the data.
Understanding how properties and methods work helps you explain how your code handles data efficiently and clearly.
"What if the method loops through the entire property array instead of accessing one element? How would the time complexity change?"
Practice
properties in a class definition?Solution
Step 1: Understand what properties represent
Properties in MATLAB classes are used to hold data or attributes about the object.Step 2: Differentiate properties from methods
Methods are functions that act on the data, not the data itself.Final Answer:
To store data related to the object -> Option CQuick Check:
Properties = Data storage [OK]
- Confusing properties with methods
- Thinking properties are for functions
- Assuming properties handle errors
Solution
Step 1: Identify method block syntax
Methods in MATLAB classes are defined inside amethodsblock.Step 2: Check function definition inside methods
The function must have the object as input and output if modifying properties.Final Answer:
methods function obj = setValue(obj, val) obj.Value = val; end end -> Option AQuick Check:
Methods block + function syntax = methods function obj = setValue(obj, val) obj.Value = val; end end [OK]
- Placing functions inside properties block
- Omitting the methods keyword
- Not returning the modified object
classdef MyClass
properties
Value
end
methods
function obj = setValue(obj, val)
obj.Value = val;
end
function v = getValue(obj)
v = obj.Value;
end
end
end
obj = MyClass();
obj = obj.setValue(10);
disp(obj.getValue());What will be the output when this code runs?
Solution
Step 1: Understand method calls and property assignment
The methodsetValuesets the propertyValueto 10 and returns the updated object.Step 2: Check the output of
ThegetValuegetValuemethod returns the current value of the propertyValue, which is 10.Final Answer:
10 -> Option DQuick Check:
Property set and get returns 10 [OK]
- Forgetting to assign returned object from setValue
- Expecting property access without method
- Confusing property name with variable
methods
function setValue(obj, val)
obj.Value = val;
end
endSolution
Step 1: Check method signature for modifying properties
In MATLAB, when a method modifies object properties, it must return the updated object.Step 2: Identify missing return value
The methodsetValuedoes not returnobj, so changes won't persist.Final Answer:
The method must return the modified object -> Option AQuick Check:
Modify method must return object [OK]
- Not returning the object after modification
- Assuming properties change without return
- Confusing function keyword usage
BankAccount with a property Balance and a method deposit that adds money to the balance only if the amount is positive. Which of the following method definitions correctly implements this?Solution
Step 1: Check method signature for modifying object
The method must accept the object and amount, and return the updated object.Step 2: Verify condition for positive amount
The method correctly adds amount only if it is greater than zero.Step 3: Identify incorrect options
methods function deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end does not return the object, so changes won't persist. methods function obj = deposit(amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end misses the object input. methods function obj = deposit(obj, amount) obj.Balance = obj.Balance + amount; end end adds amount without checking if positive.Final Answer:
methods function obj = deposit(obj, amount) if amount > 0 obj.Balance = obj.Balance + amount; end end end -> Option BQuick Check:
Return updated object and check amount > 0 [OK]
- Not returning updated object
- Missing object input argument
- Not checking if amount is positive
Ready to prove it?
Pass the test and this pattern moves unknown
