Empty Arrays - MATLAB & Simulink - MathWorks 中国 (2024)

Open Live Script

An empty array in MATLAB® is an array that has no elements. Empty arrays are useful for representing the concept of "nothing" in programming. Empty arrays have specific dimensions, and at least one of those dimensions is 0. The simplest empty array is 0-by-0, but empty arrays can have some nonzero dimensions, such as 0-by-5 or 3-by-0-by-5.

Creating Empty Arrays

To create a 0-by-0 array, use square brackets without specifying any values. Verify the array dimensions by using the size function.

A = []
A = []
size(A)
ans = 1×2 0 0

In most cases where you need to create an empty array, a 0-by-0 array is sufficient. However, you can create empty arrays of different sizes by using functions like zeros or ones. For example, create a 0-by-5 matrix and a 3-by-0-by-5 array.

B = zeros(0,5)
B = 0x5 empty double matrix
C = ones(3,0,5)
C = 3x0x5 empty double array

The square brackets, zeros, and ones create empty numeric arrays of the default double data type. To create empty arrays that work with text as data, use the strings function. This function creates a string array of any specified size with no characters. For example, create a 0-by-5 empty string array.

S = strings(0,5)
S = 0x5 empty string array

To create empty arrays of any data type, you can use createArray (since R2024a). For example, create a 5-by-0 matrix of 8-bit signed integer type int8.

D = createArray(5,0,"int8")
D = 5x0 empty int8 matrix

Empty Results from Operations

Some operations can return empty arrays. These empty arrays are useful because they can flow through algorithms without requiring special-case handling.

For example, find all elements of a row vector that are less than 0 by using the find function. If all elements of the vector are greater than 0, then find returns an empty row vector of indices because no elements meet the specified condition.

A = [1 2 3 4];ind = find(A < 0)
ind = 1x0 empty double row vector

Indexing with the empty row vector also returns an empty row vector.

B = A(ind)
B = 1x0 empty double row vector

Empty results can also be matrices or multidimensional arrays. For example, this code extracts a submatrix B from a matrix A, where B contains all nonzero rows of A.

A = zeros(5,3);dim = 2;B = A(any(A,dim),:)
B = 0x3 empty double matrix

For any matrix A, B has a size of p-by-q, where p is the number of rows of A that have any nonzero values, and q is the number of columns of A. If no rows in A have nonzero values, then B is an empty array of size 0-by-q. Because B can be an empty array, you can find the number of nonzero rows in A without special handling for the empty case.

n_nonzero_rows = size(B,1)
n_nonzero_rows = 0

If you want to explicitly check for empty results, you can use the isempty function.

if isempty(B) disp("All rows of A contain all zeros.")else disp("There are " + size(B,1) + " rows in A with nonzero values.")end
All rows of A contain all zeros.

Operations on Empty Arrays

Any operation that is defined for regular matrices and arrays extends to empty arrays. Operations can involve m-by-n arrays, even when m or n is zero. The sizes of the results of such operations are consistent with the sizes of the results generated when working with nonempty arrays.

Arrays with compatible sizes automatically expand to be the same size during the execution of element-wise operations. For more information, see Compatible Array Sizes for Basic Operations.

For example, the plus operator (+) is an element-wise operator. If b is a scalar, then A + b produces an output that is the same size as A, even if A is empty.

A = zeros(1,0,2)
A = 1x0x2 empty double array
C = A + 5
C = 1x0x2 empty double array

Similarly, if one input is an empty matrix A and the other is a column vector B with the same number of rows as A, then A + B produces an output that is the same size as A.

A = zeros(4,0)
A = 4x0 empty double matrix
B = ones(4,1)
B = 4×1 1 1 1 1
C = A + B
C = 4x0 empty double matrix

However, some functions operate vector-wise, such as the prod and sum functions. These functions produce a scalar output for a vector input or a vector output for a matrix input. These functions follow mathematical conventions when the inputs involve empty arrays.

For a nonempty input matrix, the prod function is a vector-wise operation on each column vector of the input matrix. For an empty input array, the prod function returns 1 because an empty product is the result of multiplying with no factors. By mathematical convention, the result of this product is equal to the multiplicative identity. For example, find the product of the elements in each column of nonempty and empty matrices.

C = prod([1 2 3; 2 3 4; 3 4 5])
C = 1×3 6 24 60
C = prod([])
C = 1
C = prod(zeros(0,3))
C = 1×3 1 1 1

Concatenating Empty Arrays

You can use empty arrays when storing and organizing data if the data does not have specific initial values. You can dynamically grow the empty arrays by concatenating them with nonempty arrays.

When you concatenate an empty array with a nonempty array, MATLAB omits the empty array and neglects its dimensions in the output. For example, create an empty array named data and then expand it in a loop.

data = [];for k = 1:3 data = [data rand(1,2)]end
data = 1×2 0.8147 0.9058
data = 1×4 0.8147 0.9058 0.1270 0.9134
data = 1×6 0.8147 0.9058 0.1270 0.9134 0.6324 0.0975

In the first loop iteration, the empty array of size 0-by-0 is concatenated with a nonempty array of size 1-by-2, resulting in an output of size 1-by-2. Each iteration adds new data to the array.

When you concatenate two empty arrays that have compatible sizes, the result is an empty array whose size is equal to the output size as if the inputs are nonempty. For example, create an empty array of size 0-by-5. Concatenate two of these arrays horizontally and vertically to create other arrays. The results are empty arrays with sizes 0-by-10 and 0-by-5, respectively.

A = zeros(0,5);B_horizontal = [A A]
B_horizontal = 0x10 empty double matrix
B_vertical = [A; A]
B_vertical = 0x5 empty double matrix

Empty Arrays as Placeholder Input Arguments

Some MATLAB functions accept empty arrays as placeholder input arguments.

For example, the max function finds the maximum elements of an array. If A is a matrix, you can use max(A) to find the maximum value of each column of A.

A = [1 7 3; 6 2 9];m = max(A)
m = 1×3 6 7 9

If you specify a nonempty second input to the max function, it returns the largest elements from the two inputs.

B = [5 5 5; 8 8 8];M = max(A,B)
M = 2×3 5 7 5 8 8 9

To find the maximum over all elements of A, you can pass an empty array as the second argument of max, and then specify "all".

m = max(A,[],"all")
m = 9

Removing Rows or Columns Using Square Brackets

The syntax for removing a row or column from an existing matrix also uses square brackets.

For example, remove the third column from a matrix.

A = magic(3)
A = 3×3 8 1 6 3 5 7 4 9 2
A(:,3) = []
A = 3×2 8 1 3 5 4 9

In this context, [] does not represent an empty array of size 0-by-0. If you assign an empty array to A(:,3), such as in A(:,3) = zeros(0,0), the code returns an error because the sizes of the left side and the right side of the assignment do not match.

Related Topics

  • Creating, Concatenating, and Expanding Matrices
  • Compatible Array Sizes for Basic Operations
  • Removing Rows or Columns from a Matrix
Empty Arrays
- MATLAB & Simulink
- MathWorks 中国 (2024)

FAQs

How to detect empty array in MATLAB? ›

Description. TF = isempty( A ) returns logical 1 ( true ) if A is empty, and logical 0 ( false ) otherwise. An empty array, table, or timetable has at least one dimension with length 0, such as 0-by-0 or 0-by-5.

What is an empty array in MATLAB? ›

In MATLAB, an empty array is an array that has at least one dimension of size 0. An empty array has no elements. The empty method enables you to initialize arrays of a specific class. You can expand an empty array into a nonempty array by assigning a specific value into the empty array.

How do you create an array of empty objects in MATLAB? ›

Creating Empty Arrays

To create a 0-by-0 array, use square brackets without specifying any values. Verify the array dimensions by using the size function. In most cases where you need to create an empty array, a 0-by-0 array is sufficient.

How do you create an empty numeric array in MATLAB? ›

The square brackets, zeros , and ones create empty numeric arrays of the default double data type. To create empty arrays that work with text as data, use the strings function. This function creates a string array of any specified size with no characters. For example, create a 0-by-5 empty string array.

How to check if array is empty? ›

The most straightforward way to check if an array is empty is by using the length property. If the length is 0 , the array is empty. const array = []; if (array. length === 0) { console.

How to check if post array is empty? ›

How to Check If an Array Is Empty in PHP
  1. Using the === [] syntax.
  2. Using the empty() function.
  3. Using the count() function.
  4. Using the sizeof() function.
  5. Laravel tip: Using the blank() helper function.
Jan 8, 2024

How do you fix an empty array? ›

Empty array errors occur when an array formula returns an empty set. For example, =FILTER(C3:D5,D3:D5<100) will return an error because there are no values less than 100 in our data set. To resolve the error, either change the criterion, or add the if_empty argument to the FILTER function.

How to define an empty array? ›

If you don't want to hard code the data immediately when you initialize the array, you can instead create an empty array (with all values initialized to zero) as follows. int[] myArray = new int[3]; Here, you have to explicitly give the size of the array.

What do empty arrays contain? ›

Empty arrays are useful for representing the concept of "nothing" in programming. Empty arrays have specific dimensions, and at least one of those dimensions is 0. The simplest empty array is 0-by-0, but empty arrays can have some nonzero dimensions, such as 0-by-5 or 3-by-0-by-5.

How do you create an empty array of cells in MATLAB? ›

You also can use the {} operator to create an empty 0-by-0 cell array. When you want to add values to a cell array over time or in a loop, first create an empty array using the cell function. This approach preallocates memory for the cell array header. Each cell contains an empty array [] .

How do you create an empty structure array in MATLAB? ›

s = struct([]) creates an empty (0-by-0) structure with no fields.

How to create an array in MATLAB? ›

To create an array with multiple elements in a single row, separate the elements with either a comma ',' or a space. This type of array is called a row vector. To create an array with multiple elements in a single column, separate the elements with semicolons ';'. This type of array is called a column vector.

What is an empty element in MATLAB? ›

MATLAB allows for empty arrays that have dimensions with nonzero sizes, as long as at least one dimension is 0. These empty arrays, such as a 0-by-5 array, can arise naturally in many iterative algorithms, and they follow the same rules as 0-by-0 empty arrays. The array has a class but does not contain any elements.

How do you create an empty character array? ›

We can empty a char array in C by setting all of the elements to '\0'. So, we can change every index element to '\0', as the first element is '\0' so the char array[] will be treated as empty.

How to fill array with 0 in MATLAB? ›

X = zeros(___,'like', p ) returns an array of zeros like p ; that is, of the same data type (class), sparsity, and complexity (real or complex) as p . You can specify typename or 'like' , but not both.

How do you test for an empty variable in MATLAB? ›

In MATLAB, you can check if a variable is not defined by using the exist() function. The exist() function takes a string input that specifies the name of the variable, and it returns a value that indicates whether the variable exists or not.

How to detect empty vector in MATLAB? ›

isempty(sys) returns a logical value of 1 ( true ) if the dynamic system model sys has no input or no output, and a logical value of 0 ( false ) otherwise. Where sys is a frd model, isempty(sys) returns 1 when the frequency vector is empty.

How do you identify an empty string in MATLAB? ›

Empty strings contain zero characters and display as double quotes with nothing between them ( "" ). You can determine if a string is an empty string using the == operator.

How to check if a matrix is empty? ›

isempty(x) returns true if x is an empty matrix or an empty list. isempty() can be overloaded for custom t-lists and m-lists typeof.

References

Top Articles
Latest Posts
Article information

Author: Horacio Brakus JD

Last Updated:

Views: 5778

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Horacio Brakus JD

Birthday: 1999-08-21

Address: Apt. 524 43384 Minnie Prairie, South Edda, MA 62804

Phone: +5931039998219

Job: Sales Strategist

Hobby: Sculling, Kitesurfing, Orienteering, Painting, Computer programming, Creative writing, Scuba diving

Introduction: My name is Horacio Brakus JD, I am a lively, splendid, jolly, vivacious, vast, cheerful, agreeable person who loves writing and wants to share my knowledge and understanding with you.