Free MATLAB Data Types and Structures Questions and Answers — Questions and Answers
Question 1: An engineer needs to store data for multiple electronic components. Each component has a text-based part number (e.g., 'R-101'), a numeric resistance value (e.g., 4700), and a 1x2 vector for its physical dimensions (e.g., [0.25, 0.125]). Which data structure is most suitable for creating an array to hold all the information for 50 such components?
- A cell array
- A multidimensional numeric array
- A structure array (Correct answer)
- A table with mixed data types
Correct answer: A structure array
A structure array is ideal for this scenario. Each element of the array can be a struct representing one component, with named fields like 'PartNumber', 'Resistance', and 'Dimensions' holding the different data types. This keeps the related data for each component neatly organized.
Question 2: A data analyst has a list of experimental conditions stored in a string array: `conditions = ["Low", "Medium", "High", "Medium", "Low"];`. To improve memory efficiency and enable mathematical comparisons (e.g., 'Low' < 'Medium'), which data type conversion is most appropriate?
- Convert to a numeric array using `double()`
- Convert to a cell array of character vectors
- Convert to an ordinal `categorical` array (Correct answer)
- Convert to a `table` with one variable
Correct answer: Convert to an ordinal `categorical` array
Converting to an ordinal `categorical` array is the best choice. Categorical arrays are memory-efficient because they store integer pointers to a single copy of each unique category name. Specifying them as 'ordinal' allows for defining a mathematical order ('Low' < 'Medium' < 'High'), which is not possible with string or cell arrays.
Question 3: A programmer creates two text variables as follows: `text1 = 'Results';` `text2 = "Results";` What are the data types (classes) of `text1` and `text2` respectively?
- `string` and `char`
- `char` and `string` (Correct answer)
- Both are `char`
- Both are `string`
Correct answer: `char` and `string`
In MATLAB, single quotes `' '` are used to create a character array (class `char`). Double quotes `" "` are used to create a string scalar or string array (class `string`). Therefore, `text1` is a `char` array and `text2` is a `string`.
Question 4: A programmer needs to create a single container variable that can hold the following three items: a 4x4 magic square, the character vector 'Experiment A', and a `datetime` object. Which of the following commands correctly creates such a container?
- C = [magic(4); 'Experiment A'; datetime('now')]
- C = {magic(4), 'Experiment A', datetime('now')} (Correct answer)
- C = struct('Data', magic(4), 'Name', 'Experiment A', 'Time', datetime('now'))
- C = table(magic(4), 'Experiment A', datetime('now'))
Correct answer: C = {magic(4), 'Experiment A', datetime('now')}
A cell array, created using curly braces `{}`, is the only data type among the choices that can store completely different data types and sizes (a 4x4 matrix, a 1x12 char array, and a 1x1 datetime object) as individual elements in a simple 1x3 array.
Question 5: Given a table `T` created with the command `T = table([8; 10; 11], ["A"; "B"; "B"], 'VariableNames', {'Score', 'Group'})`. Which command correctly extracts the 'Score' value from the row where 'Group' is equal to "A"?
- T.Score(T.Group == "A") (Correct answer)
- T('A', 'Score')
- T{T.Group("A"), 'Score'}
- T(1, 1)
Correct answer: T.Score(T.Group == "A")
The expression `T.Group == "A"` creates a logical index `[1; 0; 0]`. Using this logical index to index into the 'Score' variable `T.Score` correctly selects only the elements where the condition is true, returning the value `8`.
Question 6: A financial analyst has two `datetime` objects: `d1 = datetime('2025-01-10');` and `d2 = datetime('2025-03-15');`. To find the total number of calendar days that have passed between these two dates, which is the most direct command?
- days(d2 - d1)
- caldays(d2 - d1)
- d2 - d1 (Correct answer)
- between(d1, d2)
Correct answer: d2 - d1
Directly subtracting one datetime object from another (`d2 - d1`) results in a `duration` object that represents the exact time elapsed. For dates without a time component, this duration is conveniently expressed in days. While other functions can work, direct subtraction is the most fundamental operation for this task.
An engineer needs to store data for multiple electronic components.
Each component has a text-based part number (e.g., 'R-101'), a numeric resistance value (e.g., 4700), and a 1x2 vector for its physical dimensions (e.g., [0.25, 0.125]).
Which data structure is most suitable for creating an array to hold all the information for 50 such components?