MATLAB Data Types and Structures 4 — Questions and Answers
Question 1: What does the `containers.Map` class provide in MATLAB?
- A GUI mapping tool
- A key-value data structure similar to a dictionary or hash map (Correct answer)
- A function for geographical mapping
- A matrix container with named rows
Correct answer: A key-value data structure similar to a dictionary or hash map
`containers.Map` implements a hash map allowing lookup of values by arbitrary key types.
Question 2: How do you remove a field `z` from struct `s` in MATLAB?
- s.z = []
- delete(s.z)
- rmfield(s, 'z') (Correct answer)
- s = removefield(s, 'z')
Correct answer: rmfield(s, 'z')
`rmfield(s, 'z')` returns a new struct without the field `z`; it does not modify `s` in place.
Question 3: What is the output of `class(true)` in MATLAB?
- 'bool'
- 'int1'
- 'logical' (Correct answer)
- 'boolean'
Correct answer: 'logical'
MATLAB's Boolean type is called `logical`, so `class(true)` returns `'logical'`.
Question 4: Which function checks if all elements in a logical array are true?
- any()
- all() (Correct answer)
- every()
- alltrue()
Correct answer: all()
`all()` returns true only if every element is nonzero/true, while `any()` returns true if at least one element is.
Question 5: What is the byte size of a `double` value in MATLAB?
- 4 bytes
- 8 bytes (Correct answer)
- 16 bytes
- 2 bytes
Correct answer: 8 bytes
`double` is a 64-bit (8-byte) floating-point type conforming to IEEE 754.
Question 6: How do you vertically concatenate two cell arrays `A` and `B` of equal column count?
- [A, B]
- [A; B] (Correct answer)
- cat(A, B)
- vertcat(A, B, 1)
Correct answer: [A; B]
The semicolon in `[A; B]` concatenates cell arrays vertically, just like numeric arrays.
Question 7: Which MATLAB function returns the data type of a variable as a character vector?
- typeof()
- dtype()
- class() (Correct answer)
- type()
Correct answer: class()
`class(x)` returns a character vector such as `'double'`, `'char'`, or `'cell'` describing the variable's type.
What does the `containers.Map` class provide in MATLAB?