Challenge - 5 Problems
Heterogeneous Containers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of cell array with mixed types
What is the output of this MATLAB code that uses a cell array to store different types of data?
MATLAB
C = {42, 'hello', [1, 2, 3]};
disp(C{2});Attempts:
2 left
💡 Hint
Remember that cell arrays can hold different types, and disp shows the content of the cell.
✗ Incorrect
The cell array C holds a number, a string, and a numeric array. Accessing C{2} returns the string 'hello'.
🧠 Conceptual
intermediate1:30remaining
Why use heterogeneous containers in MATLAB?
Why do we need heterogeneous containers like cell arrays in MATLAB instead of regular numeric arrays?
Attempts:
2 left
💡 Hint
Think about what happens if you try to mix numbers and strings in a normal array.
✗ Incorrect
Regular numeric arrays require all elements to be the same type. Cell arrays allow storing different types together.
🔧 Debug
advanced1:30remaining
Identify the error when mixing types in a numeric array
What error does this MATLAB code produce?
MATLAB
A = [1, 'text', 3];
Attempts:
2 left
💡 Hint
MATLAB tries to convert all elements to the same type in numeric arrays.
✗ Incorrect
The character 'text' cannot be converted to a double automatically, causing a conversion error.
🚀 Application
advanced2:00remaining
Using heterogeneous containers to store mixed data
You want to store a person's name (string), age (number), and scores (numeric array) together. Which MATLAB container is best?
Attempts:
2 left
💡 Hint
Think about which container can hold different types together easily.
✗ Incorrect
Cell arrays can hold mixed types in one container. Structures also work but are not asked here.
❓ Predict Output
expert2:30remaining
Output of nested heterogeneous containers
What is the output of this MATLAB code?
MATLAB
C = {42, {'nested', 3.14}, 'end'};
disp(C{2}{1});Attempts:
2 left
💡 Hint
Remember that C{2} is itself a cell array, so you can index it again.
✗ Incorrect
C{2} is the cell array {'nested', 3.14}, so C{2}{1} returns 'nested'.