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

NameTypeRequiredDefaultDescription
AAnyYesInput numeric array.
pAnyNo2Positive norm order or Inf.
dimAnyNoDimension to operate along.

Returns

NameTypeDescription
NNumericArrayVector-wise norm values.

Errors

IdentifierWhenMessage
RunMat:vecnorm:InvalidArgumentThe norm order or dimension argument is malformed or unsupported.vecnorm: invalid argument
RunMat:vecnorm:InvalidInputInput values cannot be converted to a supported numeric array domain.vecnorm: invalid input
RunMat:vecnorm:InternalRuntime fails while reducing, allocating, gathering, or uploading values.vecnorm: internal runtime failure

How vecnorm works

  • The output has the same shape as A except the operated dimension has length 1.
  • p may be any positive real scalar or Inf.
  • Complex inputs are reduced using element magnitudes and return real non-negative values.
  • NaN values propagate within the vector being reduced.
  • When dim is greater than ndims(A), or size(A,dim) is 1, the result is abs(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 = 36

Reducing complex values by magnitude

z = [3+4i; 5+12i];
N = vecnorm(z)

Expected output:

N = 13.9284

Operating along a trailing dimension

A = reshape(1:8, [2 2 2]);
N = vecnorm(A, 2, 3)

Expected output:

N is size 2x2x1

Keeping 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.

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how vecnorm is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.