Problem 58349. Create an empty array
Suppose you need an empty array.
e = [] will give you one, but it's a double array, which may not help if you need a different type: e(1) = "Hello, world!" will (silently!) set e(1) to NaN, and e(1) = { @sin } will fail outright with an error message.
If you know the type you want, you can get around this easily. For instance, e = cell(0) (or e = cell.empty) will give you an empty cell array, e = string.empty will give you an empty string array, and in general you can do e = <type>.empty for most types (there are exceptions, such as graph, digraph, dictionary etc., which can only be instantiated as scalars).
But what if you don't know the type, and instead need to match a given array dynamically? Well, that's for you to figure out: create a function that accomplishes this: given an array A, create a function that returns an empty array (as determined by isempty()) with type matching that of A.
EDIT: the collection of types in the test suite is not exhaustive; your function should work for any type, and the test suite may change in response to hard-coded solutions.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers6
Suggested Problems
-
Determine whether a vector is monotonically increasing
20219 Solvers
-
541 Solvers
-
The sum of individual numbers...
81 Solvers
-
interesting center of magic matrix
105 Solvers
-
Create the following sequence : 0 1 1 4 9 25 64 169 ...
178 Solvers
More from this Author14
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!