isscalar — Test whether values are scalars in MATLAB and RunMat.
isscalar(A) returns true when A has exactly one element with unit dimensions. Shape handling across value types follows MATLAB semantics.
Syntax
tf = isscalar(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input value to inspect. |
Returns
| Name | Type | Description |
|---|---|---|
tf | LogicalArray | True when input has exactly one element and unit dimensions. |
How isscalar works
isscalaris true if and only ifnumel(A) == 1and every entry ofsize(A)equals1.- Numeric, logical, and complex scalars (
42,true,3+4i) satisfy both conditions. - Row, column, or higher-dimensional vectors (e.g.
[1 2 3],zeros(2,1)) are not scalar because at least one dimension exceeds one. - String scalars (
"hello","") are scalar because they are1×1string arrays. Character arrays must be1×1to count as scalar—'h'is scalar, while'runmat'(1×6) is not. - Cell arrays, structs, objects, and handle objects are scalar when their MATLAB dimensions are
1×1, regardless of the contents they wrap. - Empty arrays (
[],cell(0,1), strings of size0×1) are not scalar because they contain zero elements even if some dimensions equal one. - GPU tensors rely on the shape metadata stored in their
GpuTensorHandle. If a provider omits that metadata, RunMat gathers once to confirm the dimensions before answering.
Does RunMat run isscalar on the GPU?
isscalar never launches GPU kernels. For gpuArray inputs the builtin first inspects the shape stored in the GpuTensorHandle. When the active acceleration provider omits that metadata, RunMat performs a single gather to compute numel and dimensions on the host. The builtin always returns a host logical scalar, so fusion planning treats it as a metadata query instead of a device-side kernel.
Examples
Checking if a numeric value is scalar
tf = isscalar(42)Expected output:
tf = logical(1)Detecting that a row vector is not scalar
tf = isscalar([1 2 3])Expected output:
tf = logical(0)Verifying scalar status of string vs char arrays
tf_string = isscalar("hello");
tf_char = isscalar('h');
tf_char_row = isscalar('runmat')Expected output:
tf_string = logical(1)
tf_char = logical(1)
tf_char_row = logical(0)Identifying that an empty array is not scalar
tf_empty = isscalar([])Expected output:
tf_empty = logical(0)Confirming that single-element cell arrays are scalar
C = {pi};
tf_cell = isscalar(C)Expected output:
tf_cell = logical(1)Inspecting a GPU-resident tensor
G = gpuArray(ones(1,1));
tf_gpu = isscalar(G)Expected output:
tf_gpu = logical(1)Using isscalar with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how isscalar changes the result.
Run a small isscalar example, explain the result, then change one input and compare the output.
FAQ
Does isscalar([]) return true?⌄
No. Empty arrays contain zero elements, so isscalar([]) returns false.
Are string scalars considered scalar even when the text is empty?⌄
Yes. A string scalar is a 1×1 array regardless of its text, so isscalar("") returns true.
How does isscalar treat GPU arrays?⌄
It checks the metadata exposed by the GpuTensorHandle. If the provider omits shape metadata, RunMat downloads the tensor once to obtain it and then answers on the host.
What about objects or structs?⌄
Objects and structs are scalar when their array dimensions are 1×1. The contents do not affect the result.
Is a 1-by-1-by-1 array scalar?⌄
Yes. Any array whose numel equals 1 and whose dimensions are all ones is considered scalar.
Do character arrays behave differently from strings?⌄
Yes. Character arrays reflect their matrix dimensions. 'abc' is 1×3, so isscalar('abc') returns false.
Will isscalar trigger GPU computation?⌄
No. It is a metadata query and never launches GPU kernels. At most it might gather data when metadata is unavailable.
Can I rely on isscalar inside fused expressions?⌄
Yes. The builtin returns a host logical scalar and the fusion planner treats it as a metadata operation.
Related Array functions
Shape
cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how isscalar is executed, line by line, in Rust.
- View the source for isscalar in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.