isempty — Return true when an array has zero elements, matching MATLAB semantics.
isempty(A) returns logical true when the MATLAB value A contains zero elements. It mirrors MATLAB's behaviour across numeric arrays, logical arrays, character arrays, string arrays, cell arrays, structs, GPU tensors, and handle-like values.
How isempty works in RunMat
- Arrays are empty when any dimension is zero (
prod(size(A)) == 0). - Character arrays report empty when either dimension is zero. String scalars are never empty (
""is a1×1string array whose content may be empty, but it still counts as one element). - Cell arrays, struct arrays, and tables (future work) follow their MATLAB dimensions;
cell(0, n)is empty, whilecell(1, 1)is not. - Scalars, logical values, numeric scalars, function handles, objects, and handle objects always return
falsebecause they occupy one element. - GPU tensors rely on device-provided shape metadata to avoid unnecessary transfers. If metadata is missing, RunMat gathers once to confirm the shape.
How isempty runs on the GPU
isempty does not launch GPU kernels. For GPU-resident tensors (gpuArray values), RunMat inspects the shape stored in the GpuTensorHandle. When the active provider omits that metadata, the runtime downloads the tensor once to maintain MATLAB-compatible results. The builtin always returns a host logical scalar and does not allocate device memory, so it is safe in fused GPU expressions.
Examples
Checking if a matrix has any elements
A = zeros(0, 3);
tf = isempty(A)Expected output:
tf = logical(1)Detecting an empty cell array
C = cell(0, 4);
tf = isempty(C)Expected output:
tf = logical(1)Using isempty on a GPU-resident tensor
G = gpuArray(zeros(5, 0));
tf = isempty(G)Expected output:
tf = logical(1)Distinguishing char arrays from string scalars
chars = '';
tf_chars = isempty(chars);
str = "";
tf_str = isempty(str)Expected output:
tf_chars = logical(1)
tf_str = logical(0)Confirming that scalars are never empty
value = 42;
tf = isempty(value)Expected output:
tf = logical(0)Inspecting empty string arrays
S = strings(0, 2);
tf = isempty(S)Expected output:
tf = logical(1)FAQ
Does isempty gather GPU data?
Only when the provider fails to populate shape metadata on the GPU handle. Otherwise, it answers using the metadata and avoids transfers.
Why is isempty("") false but isempty('') true?
String scalars are 1×1 arrays that hold text content, so they are not empty even when the text has length zero. Character arrays store individual characters; the empty char literal '' has zero columns, so it is empty.
How does isempty treat structs and objects?
Structs follow their array dimensions. A scalar struct (1×1) is not empty, while a 0×0 struct array is empty. Value objects and handle objects are always treated as scalar values, so isempty returns false.
Can isempty be used inside GPU-fused expressions?
Yes. The builtin returns a host logical scalar and does not allocate GPU buffers, so fusion plans remain valid.
Does isempty look inside cell arrays?
No. It only checks the container dimensions. To look at the contents, inspect individual cells.
What does isempty return for logical or numeric scalars?
They behave like any other scalar and return false.
Related functions to explore
These functions work well alongside isempty. Each page has runnable examples you can try in the browser.
numel, size, length, gpuArray, gather, ismatrix, isscalar, isvector, ndims
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isempty works, line by line, in Rust.
- View isempty.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.