What does the isscalar function do in MATLAB / RunMat?
isscalar(A) returns logical true when an input has exactly one element and every visible
dimension equals one. The builtin mirrors MATLAB behaviour across numeric values, logical arrays,
strings, character arrays, cells, structs, GPU tensors, and handle-like values.
How does the isscalar function behave in MATLAB / RunMat?
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.
isscalar Function GPU Execution Behaviour
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 of using the isscalar function in MATLAB / RunMat
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)
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.