Structures and field access in MATLAB - Time & Space Complexity
We want to understand how the time it takes to access data in a structure grows as the structure gets bigger.
Specifically, how does accessing fields in a MATLAB structure change with more data?
Analyze the time complexity of the following code snippet.
data = struct();
for i = 1:n
data(i).value = i * 2;
end
for i = 1:n
x = data(i).value;
end
This code creates a structure array with n elements, each having a field 'value'. Then it accesses each field once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the structure array to access the 'value' field.
- How many times: Exactly n times, once for each element in the structure array.
As n grows, the number of field accesses grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 field accesses |
| 100 | 100 field accesses |
| 1000 | 1000 field accesses |
Pattern observation: The time grows in a straight line as the number of elements increases.
Time Complexity: O(n)
This means the time to access all fields grows directly in proportion to the number of elements.
[X] Wrong: "Accessing a field in a structure is instant and does not depend on the number of elements."
[OK] Correct: While accessing one field is quick, doing it repeatedly for many elements adds up, so total time grows with the number of elements.
Knowing how data access time grows helps you write efficient code and explain your choices clearly in interviews.
"What if we accessed multiple fields inside the loop instead of just one? How would the time complexity change?"
Practice
age from a structure person in MATLAB?Solution
Step 1: Understand MATLAB structure field access
In MATLAB, fields of a structure are accessed using dot notation without parentheses or brackets.Step 2: Identify correct syntax for field access
The correct syntax isstructure.field, soperson.ageis correct.Final Answer:
person.age -> Option BQuick Check:
Use dot notation for fields [OK]
- Using parentheses like a function call
- Using brackets like a dictionary
- Using arrow notation from other languages
car with fields make and year in MATLAB?Solution
Step 1: Recall MATLAB struct creation syntax
MATLAB uses thestructfunction with pairs of field names and values as strings and values.Step 2: Match correct syntax
car = struct('make', 'Toyota', 'year', 2020); usesstruct('make', 'Toyota', 'year', 2020), which is correct MATLAB syntax.Final Answer:
car = struct('make', 'Toyota', 'year', 2020); -> Option DQuick Check:
Use struct('field', value, ...) to create structures [OK]
- Using JSON or dictionary syntax
- Using equal signs inside struct()
- Using brackets instead of parentheses
person.name = 'Alice'; person.age = 30; disp(person.age);
Solution
Step 1: Understand structure field assignment
The code assigns 'Alice' toperson.nameand 30 toperson.age.Step 2: Identify what
Thedisp(person.age)showsdispfunction prints the value ofperson.age, which is 30.Final Answer:
30 -> Option CQuick Check:
disp shows the value of the accessed field [OK]
- Confusing field names with values
- Expecting the field name instead of value
- Syntax errors from missing semicolons
data = struct('x', 10, 'y', 20);
disp(data.z);
What will happen when this code runs?Solution
Step 1: Check structure fields
The structuredatahas fieldsxandy, but no fieldz.Step 2: Accessing a non-existent field
Trying to accessdata.zcauses MATLAB to throw an error about the missing field.Final Answer:
Error: Reference to non-existent field 'z'. -> Option AQuick Check:
Accessing missing fields causes an error [OK]
- Assuming missing fields return 0 or empty
- Ignoring error messages
- Confusing field names with variable names
students with fields name and score. How do you create a new structure passed containing only students with score >= 50?Solution
Step 1: Understand structure arrays and logical indexing
In MATLAB,[students.score]creates an array of scores. Logical indexing selects elements where condition is true.Step 2: Apply logical indexing to filter students
The expressionstudents([students.score] >= 50)selects only students with scores 50 or above.Final Answer:
passed = students([students.score] >= 50); -> Option AQuick Check:
Use logical indexing on structure arrays [OK]
- Trying to assign logical array directly
- Using curly braces instead of parentheses
- Creating new struct without filtering
Ready to prove it?
Pass the test and this pattern moves unknown
