vecnorm — Compute vector-wise array norms in MATLAB and RunMat.
vecnorm(A) returns vector-wise 2-norms. Matrices reduce down columns by default, multidimensional arrays reduce along the first nonsingleton dimension, and vecnorm(A,p,dim) reduces along an explicit dimension.
Syntax
N = vecnorm(A)
N = vecnorm(A, p)
N = vecnorm(A, p, dim)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input numeric array. |
p | Any | No | 2 | Positive norm order or Inf. |
dim | Any | No | — | Dimension to operate along. |
Returns
| Name | Type | Description |
|---|---|---|
N | NumericArray | Vector-wise norm values. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:vecnorm:InvalidArgument | The norm order or dimension argument is malformed or unsupported. | vecnorm: invalid argument |
RunMat:vecnorm:InvalidInput | Input values cannot be converted to a supported numeric array domain. | vecnorm: invalid input |
RunMat:vecnorm:Internal | Runtime fails while reducing, allocating, gathering, or uploading values. | vecnorm: internal runtime failure |
How vecnorm works
- The output has the same shape as
Aexcept the operated dimension has length 1. pmay be any positive real scalar orInf.- Complex inputs are reduced using element magnitudes and return real non-negative values.
- NaN values propagate within the vector being reduced.
- When
dimis greater thanndims(A), orsize(A,dim)is 1, the result isabs(A)with the original shape.
Does RunMat run vecnorm on the GPU?
RunMat currently implements vecnorm as a GPU-aware host reduction: GPU inputs are gathered, vector-wise norms are computed with the same CPU logic as host arrays, and the result is uploaded back to the active provider when possible.
The fallback includes NaNs and preserves MATLAB shape semantics. If upload fails, RunMat returns the host tensor result rather than changing the numeric answer.
GPU memory and residency
G = gpuArray([3 5; 4 12]);
N = vecnorm(G)Expected output:
N = [5 13]Examples
Computing column-wise Euclidean norms
A = [2 0 1; -1 1 0; -3 3 0];
N = vecnorm(A)Expected output:
N = [3.7417 3.1623 1]Computing row-wise 1-norms
A = [1 3; 2 4];
N = vecnorm(A, 1, 2)Expected output:
N = [4; 6]Using a fractional positive norm order
x = [1; 4; 9];
N = vecnorm(x, 0.5)Expected output:
N = 36Reducing complex values by magnitude
z = [3+4i; 5+12i];
N = vecnorm(z)Expected output:
N = 13.9284Operating along a trailing dimension
A = reshape(1:8, [2 2 2]);
N = vecnorm(A, 2, 3)Expected output:
N is size 2x2x1Keeping GPU workflows compatible
G = gpuArray([3 5; 4 12]);
N = vecnorm(G)Expected output:
N = [5 13]Using vecnorm with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how vecnorm changes the result.
Run a small vecnorm example, explain the result, then change one input and compare the output.
FAQ
How is vecnorm different from norm?⌄
norm(A) returns one scalar for a vector or matrix. vecnorm(A) reduces each vector along one dimension and returns an array of norms.
What dimension does vecnorm(A) use by default?⌄
It uses the first dimension whose size is not 1. For ordinary matrices this means column-wise norms.
Does vecnorm support complex arrays?⌄
Yes. Complex elements are converted to magnitudes before applying the selected vector norm.
Can p be less than 1?⌄
Yes. MATLAB documents p as a positive scalar or Inf, and RunMat accepts positive fractional values.
What happens for dim beyond the rank of the input?⌄
RunMat matches MATLAB by returning abs(A) with the original shape.
Will GPU inputs stay on the GPU?⌄
When a provider is active, RunMat gathers the input to compute the norm and uploads the result back to the provider. A future provider hook can replace this with a device-side kernel without changing user code.
Related Linalg functions
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how vecnorm is executed, line by line, in Rust.
- View the source for vecnorm 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.