Data Types and Structures Flashcards
6 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Data Types and Structures flashcards as text
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?
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.
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?
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.
A programmer creates two text variables as follows: `text1 = 'Results';` `text2 = "Results";` What are the data types (classes) of `text1` and `text2` respectively?
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`.
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?
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.
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"?
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`.
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?
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.