Bird
Raised Fist0
MATLABdata~20 mins

Cell array creation in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cell Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this cell array creation code?
Consider the following MATLAB code that creates a cell array. What will be the content of C after running it?
MATLAB
C = {1, 'text', [2 3 4]; true, {5,6}, pi};
disp(C);
AA 2x3 cell array where C{1,1} is the string '1' and C{2,2} is the number 6
BA 3x2 cell array with numbers and strings mixed incorrectly causing an error
CA numeric matrix with elements 1, 'text', [2 3 4], true, {5,6}, pi
DA 2x3 cell array where C{1,2} is the string 'text' and C{2,3} is the number pi
Attempts:
2 left
💡 Hint
Remember that curly braces {} create cell arrays and each element can hold different types.
❓ Predict Output
intermediate
1:00remaining
What is the size of the cell array created here?
What is the size of the cell array C after executing this code?
C = cell(3,2);
MATLAB
C = cell(3,2);
sizeC = size(C);
A[3 2]
B[2 3]
C[1 6]
D[6 1]
Attempts:
2 left
💡 Hint
The cell function creates a cell array with the given number of rows and columns.
🔧 Debug
advanced
1:30remaining
Identify the error in this cell array creation
This code is intended to create a 1x3 cell array with different types of elements. What error will MATLAB show when running it?
C = {1, 2, 3; 4, 5};
MATLAB
C = {1, 2, 3; 4, 5};
AError: Dimensions of arrays being concatenated are not consistent.
BNo error, creates a 2x3 cell array with missing elements filled with empty cells.
CError: Cell arrays cannot contain numeric values.
DError: Missing semicolon at the end of the line.
Attempts:
2 left
💡 Hint
Check the number of elements in each row when creating a cell array with multiple rows.
🧠 Conceptual
advanced
1:30remaining
Which option correctly creates a nested cell array?
You want to create a cell array C where the second element is itself a cell array containing numbers 10 and 20. Which code does this correctly?
AC = {5; {10, 20}; 'hello'};
BC = {5, [10, 20], 'hello'};
CC = {5, {10, 20}, 'hello'};
DC = {5, {10; 20}, 'hello'};
Attempts:
2 left
💡 Hint
Use curly braces inside the cell array to create nested cells.
❓ Predict Output
expert
2:00remaining
What is the output of this complex cell array indexing?
Given the cell array C = {1, {2, 3}, 4};, what is the output of disp(C{2}{1})?
MATLAB
C = {1, {2, 3}, 4};
disp(C{2}{1});
A3
B2
C{2, 3}
DError: Cell contents reference from a non-cell array object.
Attempts:
2 left
💡 Hint
Remember that C{2} accesses the second cell content, which is itself a cell array, then {1} accesses its first element.

Practice

(1/5)
1. What is the correct way to create a cell array in MATLAB that contains a number, a string, and a logical value?
easy
A. myCell = [42, 'hello', true];
B. myCell = (42, 'hello', true);
C. myCell = {42, 'hello', true};
D. myCell = <42, 'hello', true>;

Solution

  1. Step 1: Understand cell array syntax

    Cell arrays use curly braces { } to hold mixed data types.
  2. Step 2: Identify correct syntax for mixed types

    myCell = {42, 'hello', true}; uses curly braces with different types: number, string, logical.
  3. Final Answer:

    myCell = {42, 'hello', true}; -> Option C
  4. Quick Check:

    Curly braces create cell arrays [OK]
Hint: Use curly braces { } for cell arrays, not square brackets [ ] [OK]
Common Mistakes:
  • Using square brackets instead of curly braces
  • Using parentheses instead of curly braces
  • Using angle brackets which are invalid
2. Which of the following is the correct syntax to create a 2x2 cell array with elements 1, 'a', 3.14, and false?
easy
A. C = [1, 'a'; 3.14, false];
B. C = {1, 'a'; 3.14, false};
C. C = {1; 'a'; 3.14; false};
D. C = (1, 'a'; 3.14, false);

Solution

  1. Step 1: Recall cell array 2D syntax

    Use curly braces { } with semicolons for rows and commas for columns.
  2. Step 2: Check each option

    C = {1, 'a'; 3.14, false}; correctly uses { } with two rows and two columns separated by ; and ,.
  3. Final Answer:

    C = {1, 'a'; 3.14, false}; -> Option B
  4. Quick Check:

    Curly braces + semicolon for rows = correct 2D cell array [OK]
Hint: Use semicolon to separate rows inside curly braces [OK]
Common Mistakes:
  • Using square brackets which create numeric arrays
  • Using commas instead of semicolons for rows
  • Using parentheses which are invalid for arrays
3. What is the output of the following MATLAB code?
myCell = {10, 'MATLAB'; 'false', 3.5};
disp(myCell{2,1});
medium
A. false
B. 3.5
C. 'MATLAB'
D. 10

Solution

  1. Step 1: Understand cell content access

    Using curly braces with indices accesses the content inside the cell.
  2. Step 2: Identify the element at row 2, column 1

    myCell{2,1} is the element in second row, first column, which is 'false' as a string.
  3. Final Answer:

    'false' -> Option A
  4. Quick Check:

    myCell{2,1} = 'false' [OK]
Hint: Use curly braces { } to get cell content, not parentheses ( ) [OK]
Common Mistakes:
  • Using parentheses instead of curly braces to access content
  • Confusing row and column indices
  • Expecting the whole cell instead of content
4. Consider this MATLAB code snippet:
C = {1, 2, 3; 4, 5, 6};
val = C(2,3);

What is the issue with this code if you want to get the number 6 as a value?
medium
A. C(2,3) returns a cell, not the content inside the cell
B. C(2,3) is invalid syntax for cell arrays
C. C(2,3) returns the number 6 directly
D. C(2,3) returns an error because of wrong indexing

Solution

  1. Step 1: Understand difference between () and {}

    Parentheses () return a cell array subset, curly braces {} return content.
  2. Step 2: Analyze C(2,3)

    C(2,3) returns a 1x1 cell containing 6, not the number 6 itself.
  3. Final Answer:

    C(2,3) returns a cell, not the content inside the cell -> Option A
  4. Quick Check:

    Use curly braces to get content: C{2,3} [OK]
Hint: Use curly braces { } to get cell content, parentheses () return cells [OK]
Common Mistakes:
  • Using parentheses expecting content instead of cell
  • Confusing cell array indexing with numeric array indexing
  • Expecting direct numeric output from parentheses indexing
5. You want to create a cell array that stores a numeric vector, a string, and another cell array inside it. Which of the following correctly creates such a nested cell array?
hard
A. nestedCell = {[1, 2, 3], 'text', [true; false]};
B. nestedCell = {[1, 2, 3], 'text', [true, false]};
C. nestedCell = {[1, 2, 3], 'text', (true, false)};
D. nestedCell = {[1, 2, 3], 'text', {true, false}};

Solution

  1. Step 1: Identify nested cell array syntax

    To nest a cell array inside another, use curly braces { } inside the main cell array.
  2. Step 2: Check each option for correct nesting

    nestedCell = {[1, 2, 3], 'text', {true, false}}; uses {true, false} as a nested cell array inside the main cell array.
  3. Final Answer:

    nestedCell = {[1, 2, 3], 'text', {true, false}}; -> Option D
  4. Quick Check:

    Curly braces inside curly braces create nested cells [OK]
Hint: Use curly braces inside curly braces for nested cell arrays [OK]
Common Mistakes:
  • Using square brackets for nested cell arrays
  • Using parentheses which are invalid
  • Using semicolons inside nested cell arrays incorrectly
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